mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #23345 from yui-knk/warning_when_composite_primary_key_is_detected
Warn if `AR.primary_key` is called for a table who has composite prim…
This commit is contained in:
commit
a59212709c
2 changed files with 13 additions and 0 deletions
|
@ -125,6 +125,12 @@ module ActiveRecord
|
|||
# Returns just a table's primary key
|
||||
def primary_key(table_name)
|
||||
pks = primary_keys(table_name)
|
||||
warn <<-WARNING.strip_heredoc if pks.count > 1
|
||||
WARNING: Rails does not support composite primary key.
|
||||
|
||||
#{table_name} has composite primary key. Composite primary key is ignored.
|
||||
WARNING
|
||||
|
||||
pks.first if pks.one?
|
||||
end
|
||||
|
||||
|
|
|
@ -260,6 +260,13 @@ class CompositePrimaryKeyTest < ActiveRecord::TestCase
|
|||
assert_equal ["region", "code"], @connection.primary_keys("barcodes")
|
||||
end
|
||||
|
||||
def test_primary_key_issues_warning
|
||||
warning = capture(:stderr) do
|
||||
@connection.primary_key("barcodes")
|
||||
end
|
||||
assert_match(/WARNING: Rails does not support composite primary key\./, warning)
|
||||
end
|
||||
|
||||
def test_collectly_dump_composite_primary_key
|
||||
schema = dump_table_schema "barcodes"
|
||||
assert_match %r{create_table "barcodes", primary_key: \["region", "code"\]}, schema
|
||||
|
|
Loading…
Reference in a new issue