From 444c4d05fea817fcad991e79fa128b640e2e4ff1 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Sat, 30 Jan 2016 16:47:51 +0900 Subject: Warn if `AR.primary_key` is called for a table who has composite primary key If `AR.primary_key` is called for a table who has composite primary key, the method returns `nil`. This behavior sometimes generates invalid SQL. The first time developers notice to invalid SQL is when they execute SQL. This commit enables developers to know they are doing something dangerous as soon as possible. --- .../active_record/connection_adapters/abstract/schema_statements.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/lib') 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 c7aff63228..3b4ceb915e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -122,6 +122,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 -- cgit v1.2.3