diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-02-11 11:09:29 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-02-11 11:20:59 +0100 |
commit | a893718c3165aa6ef6b2b500f7922954ba21eb02 (patch) | |
tree | cbc8692b54f5b6f72040000bfd6bf78d16b5bf9a /activerecord/lib | |
parent | 9bdb083dba0778f47866fa440476b3141716de52 (diff) | |
download | rails-a893718c3165aa6ef6b2b500f7922954ba21eb02.tar.gz rails-a893718c3165aa6ef6b2b500f7922954ba21eb02.tar.bz2 rails-a893718c3165aa6ef6b2b500f7922954ba21eb02.zip |
fix `remove_reference` with `foreign_key: true` on MySQL. #18664.
MySQL rejects to remove an index which is used in a foreign key constraint:
```
ActiveRecord::StatementInvalid: Mysql2::Error: Cannot drop index 'index_copies_on_title_id': needed in a foreign key constraint: ALTER TABLE `copies` DROP `title_id`
```
Removing the constraint before removing the column (and the index) solves this problem.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb | 6 |
1 files changed, 6 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 ed32997d25..3ca1c3c49a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -662,7 +662,13 @@ module ActiveRecord # # remove_reference(:products, :supplier, polymorphic: true) # + # ====== Remove the reference with a foreign key + # + # remove_reference(:products, :user, index: true, foreign_key: true) + # def remove_reference(table_name, ref_name, options = {}) + remove_foreign_key table_name, ref_name if options[:foreign_key] + remove_column(table_name, "#{ref_name}_id") remove_column(table_name, "#{ref_name}_type") if options[:polymorphic] end |