Skip to content

Commit

Permalink
Merge pull request #29455 from kirs/remove-column-with-fk-mysql
Browse files Browse the repository at this point in the history
Remove FK together with column in MySQL
  • Loading branch information
guilleiguaran committed Jun 17, 2017
2 parents f8f792f + 675a912 commit a3f758a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Expand Up @@ -45,6 +45,13 @@ def indexes(table_name, name = nil)
indexes
end

def remove_column(table_name, column_name, type = nil, options = {})
if foreign_key_exists?(table_name, column: column_name)
remove_foreign_key(table_name, column: column_name)
end
super
end

def internal_string_options_for_primary_key
super.tap do |options|
if CHARSETS_OF_4BYTES_MAXLEN.include?(charset) && (mariadb? || version < "8.0.0")
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/migration/references_foreign_key_test.rb
Expand Up @@ -139,6 +139,16 @@ class ReferencesForeignKeyTest < ActiveRecord::TestCase
end
end

test "removing column removes foreign key" do
@connection.create_table :testings do |t|
t.references :testing_parent, index: true, foreign_key: true
end

assert_difference "@connection.foreign_keys('testings').size", -1 do
@connection.remove_column :testings, :testing_parent_id
end
end

test "foreign key methods respect pluralize_table_names" do
begin
original_pluralize_table_names = ActiveRecord::Base.pluralize_table_names
Expand Down

0 comments on commit a3f758a

Please sign in to comment.