Skip to content

Commit 39a2009

Browse files
committed
Add table to error for duplicate column definitions
If a migration defines duplicate columns for a table the error message should show which table it concerns.
1 parent 0b1b512 commit 39a2009

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

activerecord/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Add table to error for duplicate column definitions.
2+
3+
If a migration defines duplicate columns for a table, the error message
4+
shows which table it concerns.
5+
6+
*Petrik de Heus*
7+
18
* Fix erroneous nil default precision on virtual datetime columns.
29

310
Prior to this change, virtual datetime columns did not have the same

activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,9 @@ def column(name, type, index: nil, **options)
469469

470470
if @columns_hash[name]
471471
if @columns_hash[name].primary_key?
472-
raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table."
472+
raise ArgumentError, "you can't redefine the primary key column '#{name}' on '#{@name}'. To define a custom primary key, pass { id: false } to create_table."
473473
else
474-
raise ArgumentError, "you can't define an already defined column '#{name}'."
474+
raise ArgumentError, "you can't define an already defined column '#{name}' on '#{@name}'."
475475
end
476476
end
477477

activerecord/test/cases/migration/change_schema_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def test_create_table_raises_when_redefining_primary_key_column
183183
end
184184
end
185185

186-
assert_equal "you can't redefine the primary key column 'id'. To define a custom primary key, pass { id: false } to create_table.", error.message
186+
assert_equal "you can't redefine the primary key column 'id' on 'testings'. To define a custom primary key, pass { id: false } to create_table.", error.message
187187
end
188188

189189
def test_create_table_raises_when_redefining_custom_primary_key_column
@@ -193,7 +193,7 @@ def test_create_table_raises_when_redefining_custom_primary_key_column
193193
end
194194
end
195195

196-
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
196+
assert_equal "you can't redefine the primary key column 'testing_id' on 'testings'. To define a custom primary key, pass { id: false } to create_table.", error.message
197197
end
198198

199199
def test_create_table_raises_when_defining_existing_column
@@ -204,7 +204,7 @@ def test_create_table_raises_when_defining_existing_column
204204
end
205205
end
206206

207-
assert_equal "you can't define an already defined column 'testing_column'.", error.message
207+
assert_equal "you can't define an already defined column 'testing_column' on 'testings'.", error.message
208208
end
209209

210210
def test_create_table_with_timestamps_should_create_datetime_columns

0 commit comments

Comments
 (0)