diff options
author | Kir Shatrov <shatrov@me.com> | 2017-06-14 22:39:19 -0400 |
---|---|---|
committer | Kir Shatrov <shatrov@me.com> | 2017-06-16 20:49:09 -0400 |
commit | 675a912eb86a9e109e9877379e8454b388242d57 (patch) | |
tree | ecab51147d760a892965f570e790a2e6f482765d /activerecord | |
parent | f1e42fe267342ffa4cea84cf09b0caaee733632c (diff) | |
download | rails-675a912eb86a9e109e9877379e8454b388242d57.tar.gz rails-675a912eb86a9e109e9877379e8454b388242d57.tar.bz2 rails-675a912eb86a9e109e9877379e8454b388242d57.zip |
Remove FK together with column in MySQL
Unlike with other databses, MySQL doesn't let you remove the column
if there's a FK on this column.
For better developer experience we want to remove the FK together with
the column.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/migration/references_foreign_key_test.rb | 10 |
2 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb index f9e1e046ea..fc57e41fc9 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_statements.rb @@ -45,6 +45,13 @@ module ActiveRecord 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") diff --git a/activerecord/test/cases/migration/references_foreign_key_test.rb b/activerecord/test/cases/migration/references_foreign_key_test.rb index f1ddac1ee2..718b9a0613 100644 --- a/activerecord/test/cases/migration/references_foreign_key_test.rb +++ b/activerecord/test/cases/migration/references_foreign_key_test.rb @@ -139,6 +139,16 @@ if ActiveRecord::Base.connection.supports_foreign_keys? 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 |