aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-02-23 18:28:49 -0500
committerGitHub <noreply@github.com>2017-02-23 18:28:49 -0500
commit2202e81312c28c6600297bda9a66534b22f70853 (patch)
treef74bfb56c23608a95fb4f18da996cb683e3a7dc4 /activerecord
parent230cf4470bfebaa0ccd6a63d7d817c76d19f6ee5 (diff)
parent6d1440db3eb77170c461d7dab6d43a978a988fae (diff)
downloadrails-2202e81312c28c6600297bda9a66534b22f70853.tar.gz
rails-2202e81312c28c6600297bda9a66534b22f70853.tar.bz2
rails-2202e81312c28c6600297bda9a66534b22f70853.zip
Merge pull request #25285 from kamipo/fix_remove_reference_to_multiple_foreign_keys_in_the_same_table
Fix `remove_reference` to multiple foreign keys in the same table
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb1
-rw-r--r--activerecord/test/cases/migration/references_foreign_key_test.rb16
2 files changed, 17 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 c43a2d1508..c44215cd43 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -857,6 +857,7 @@ module ActiveRecord
else
foreign_key_options = { to_table: reference_name }
end
+ foreign_key_options[:column] ||= "#{ref_name}_id"
remove_foreign_key(table_name, **foreign_key_options)
end
diff --git a/activerecord/test/cases/migration/references_foreign_key_test.rb b/activerecord/test/cases/migration/references_foreign_key_test.rb
index 9418995ea0..f1ddac1ee2 100644
--- a/activerecord/test/cases/migration/references_foreign_key_test.rb
+++ b/activerecord/test/cases/migration/references_foreign_key_test.rb
@@ -203,6 +203,22 @@ if ActiveRecord::Base.connection.supports_foreign_keys?
assert_equal([["testings", "testing_parents", "parent1_id"],
["testings", "testing_parents", "parent2_id"]], fk_definitions)
end
+
+ test "multiple foreign keys can be removed to the selected one" do
+ @connection.create_table :testings do |t|
+ t.references :parent1, foreign_key: { to_table: :testing_parents }
+ t.references :parent2, foreign_key: { to_table: :testing_parents }
+ end
+
+ assert_difference "@connection.foreign_keys('testings').size", -1 do
+ @connection.remove_reference :testings, :parent1, foreign_key: { to_table: :testing_parents }
+ end
+
+ fks = @connection.foreign_keys("testings").sort_by(&:column)
+
+ fk_definitions = fks.map { |fk| [fk.from_table, fk.to_table, fk.column] }
+ assert_equal([["testings", "testing_parents", "parent2_id"]], fk_definitions)
+ end
end
end
end