From 1e01d9b9a4e3ba3c12ed45163dc35756753a7e42 Mon Sep 17 00:00:00 2001 From: Nikolay Epifanov Date: Thu, 2 Aug 2018 17:12:35 +0400 Subject: [PATCH 1/2] Fix argument handling on reverting remove_foreign_key --- activerecord/lib/active_record/migration/command_recorder.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/migration/command_recorder.rb b/activerecord/lib/active_record/migration/command_recorder.rb index 81ef4828f8133..b5b60edb8d1c8 100644 --- a/activerecord/lib/active_record/migration/command_recorder.rb +++ b/activerecord/lib/active_record/migration/command_recorder.rb @@ -214,8 +214,9 @@ def invert_add_foreign_key(args) end def invert_remove_foreign_key(args) - from_table, to_table, remove_options = args - raise ActiveRecord::IrreversibleMigration, "remove_foreign_key is only reversible if given a second table" if to_table.nil? || to_table.is_a?(Hash) + from_table, remove_options = args + to_table = remove_options.delete(:to_table) + raise ActiveRecord::IrreversibleMigration, "remove_foreign_key is only reversible if given a :to_table in options" if to_table.nil? reversed_args = [from_table, to_table] reversed_args << remove_options if remove_options From ab537d02e311afc5da81ce94f3756ce7a35518d2 Mon Sep 17 00:00:00 2001 From: Nikolay Epifanov Date: Thu, 2 Aug 2018 17:13:52 +0400 Subject: [PATCH 2/2] Add call example for remove_foreign_key --- .../connection_adapters/abstract/schema_statements.rb | 4 ++++ 1 file changed, 4 insertions(+) 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 15091471520b5..9d46eb535403b 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -980,6 +980,10 @@ def add_foreign_key(from_table, to_table, options = {}) # # remove_foreign_key :accounts, column: :owner_id # + # Same but in a reversible manner + # + # remove_foreign_key :accounts, column: :owner_id, to_table: :owners + # # Removes the foreign key named +special_fk_name+ on the +accounts+ table. # # remove_foreign_key :accounts, name: :special_fk_name