diff options
author | Eileen M. Uchitelle <eileencodes@users.noreply.github.com> | 2018-06-04 16:48:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-04 16:48:52 -0400 |
commit | c4c8e35cac419076a86112d7764f2afdef554483 (patch) | |
tree | 22cf0006dfaed42f40e52445dee1cb3e26fb3f0b | |
parent | f67b4a5b3f6136fe7af0d6e79daa593b8ab3d159 (diff) | |
parent | 7ef9849e3d51c1f4a37af368b770d8fdb41883cd (diff) | |
download | rails-c4c8e35cac419076a86112d7764f2afdef554483.tar.gz rails-c4c8e35cac419076a86112d7764f2afdef554483.tar.bz2 rails-c4c8e35cac419076a86112d7764f2afdef554483.zip |
Merge pull request #33029 from fedxgibson/raise_with_duplicate_columns
Migrations will raise an exception if there are multiple column defin…
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/migration/change_schema_test.rb | 11 |
3 files changed, 23 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index d101b81e28..a7d3319164 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Migrations raise when duplicate column definition. + + Fixes #33024. + + *Federico Martinez* + * Bump minimum SQLite version to 3.8 *Yasuo Honda* diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 5f090d16cd..582ac516c7 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -356,8 +356,12 @@ module ActiveRecord type = type.to_sym if type options = options.dup - if @columns_hash[name] && @columns_hash[name].primary_key? - raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table." + if @columns_hash[name] + if @columns_hash[name].primary_key? + raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table." + else + raise ArgumentError, "you can't define an already defined column '#{name}'." + end end index_options = options.delete(:index) diff --git a/activerecord/test/cases/migration/change_schema_test.rb b/activerecord/test/cases/migration/change_schema_test.rb index f4d16cb093..7777508349 100644 --- a/activerecord/test/cases/migration/change_schema_test.rb +++ b/activerecord/test/cases/migration/change_schema_test.rb @@ -196,6 +196,17 @@ module ActiveRecord assert_equal "you can't redefine the primary key column 'testing_id'. To define a custom primary key, pass { id: false } to create_table.", error.message end + def test_create_table_raises_when_defining_existing_column + error = assert_raise(ArgumentError) do + connection.create_table :testings do |t| + t.column :testing_column, :string + t.column :testing_column, :integer + end + end + + assert_equal "you can't define an already defined column 'testing_column'.", error.message + end + def test_create_table_with_timestamps_should_create_datetime_columns connection.create_table table_name do |t| t.timestamps |