Skip to content

Commit 7684256

Browse files
authored
Merge pull request rails#44637 from fatkodima/remove_foreign_key-if_exists
Fix `remove_foreign_key` with `:if_exists` option when foreign key actually exists
2 parents 8516bb6 + 1f70f3c commit 7684256

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Fix `remove_foreign_key` with `:if_exists` option when foreign key actually exists.
2+
3+
*fatkodima*
4+
15
* Remove `--no-comments` flag in structure dumps for PostgreSQL
26

37
This broke some apps that used custom schema comments. If you don't want

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ def add_foreign_key(from_table, to_table, **options)
11271127
# The name of the table that contains the referenced primary key.
11281128
def remove_foreign_key(from_table, to_table = nil, **options)
11291129
return unless supports_foreign_keys?
1130-
return if options[:if_exists] == true && !foreign_key_exists?(from_table, to_table)
1130+
return if options.delete(:if_exists) == true && !foreign_key_exists?(from_table, to_table)
11311131

11321132
fk_name_to_delete = foreign_key_for!(from_table, to_table: to_table, **options).name
11331133

activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def add_foreign_key(from_table, to_table, **options)
6060
end
6161

6262
def remove_foreign_key(from_table, to_table = nil, **options)
63-
return if options[:if_exists] == true && !foreign_key_exists?(from_table, to_table)
63+
return if options.delete(:if_exists) == true && !foreign_key_exists?(from_table, to_table)
6464

6565
to_table ||= options[:to_table]
6666
options = options.except(:name, :to_table, :validate)

activerecord/test/cases/migration/foreign_key_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ def test_remove_foreign_key_with_if_exists_set
668668
@connection.add_foreign_key :astronauts, :rockets
669669
assert_equal 1, @connection.foreign_keys("astronauts").size
670670

671-
@connection.remove_foreign_key :astronauts, :rockets
671+
@connection.remove_foreign_key :astronauts, :rockets, if_exists: true
672672
assert_equal [], @connection.foreign_keys("astronauts")
673673

674674
assert_nothing_raised do

0 commit comments

Comments
 (0)