diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2016-01-30 12:21:52 -0500 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2016-01-30 12:21:52 -0500 |
commit | a59212709c1daddf1c3703198cc3e45fd5226baf (patch) | |
tree | 4d008ee5a3898800de94de05d5caa66966b2d87c /activerecord | |
parent | 33848384f37b0acefc025ec72fc7896b3a3fdf88 (diff) | |
parent | 444c4d05fea817fcad991e79fa128b640e2e4ff1 (diff) | |
download | rails-a59212709c1daddf1c3703198cc3e45fd5226baf.tar.gz rails-a59212709c1daddf1c3703198cc3e45fd5226baf.tar.bz2 rails-a59212709c1daddf1c3703198cc3e45fd5226baf.zip |
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…
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/primary_keys_test.rb | 7 |
2 files changed, 13 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index c00b629632..8db7f9172f 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -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 diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 8ce2b813bf..155210cce9 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -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 |