acts-as-taggable-on icon indicating copy to clipboard operation
acts-as-taggable-on copied to clipboard

Mysql2::Error: Cannot drop index

Open kassi opened this issue 3 years ago • 5 comments

Following the installation instructions in a Ruby 3, Rails 6 application, I get the following MySQL error:

== 20210618092705 ActsAsTaggableOnMigration: migrating ========================
-- create_table(:tags, {:options=>"ENGINE=InnoDB", :id=>:integer})
   -> 0.0256s
-- create_table(:taggings, {:options=>"ENGINE=InnoDB", :id=>:integer})
   -> 0.0129s
-- add_index(:taggings, :tag_id)
   -> 0.0146s
-- add_index(:taggings, [:taggable_id, :taggable_type, :context], {:name=>"taggings_taggable_context_idx"})
   -> 0.0079s
== 20210618092705 ActsAsTaggableOnMigration: migrated (0.0620s) ===============

== 20210618092706 AddMissingUniqueIndices: migrating ==========================
-- add_index(:tags, :name, {:unique=>true})
   -> 0.0112s
-- index_exists?(:taggings, :tag_id, {:name=>"index_taggings_on_tag_id"})
   -> 0.0018s
-- remove_index(:taggings, :tag_id, {:name=>"index_taggings_on_tag_id"})
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Cannot drop index 'index_taggings_on_tag_id': needed in a foreign key constraint
/app/db/migrate/20210618092706_add_missing_unique_indices.acts_as_taggable_on_engine.rb:11:in `up'
/app/bin/rails:5:in `<top (required)>'
/app/bin/spring:15:in `<top (required)>'
/app/bin/rails:2:in `load'
/app/bin/rails:2:in `<main>'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Cannot drop index 'index_taggings_on_tag_id': needed in a foreign key constraint
/app/db/migrate/20210618092706_add_missing_unique_indices.acts_as_taggable_on_engine.rb:11:in `up'
/app/bin/rails:5:in `<top (required)>'
/app/bin/spring:15:in `<top (required)>'
/app/bin/rails:2:in `load'
/app/bin/rails:2:in `<main>'

Caused by:
Mysql2::Error: Cannot drop index 'index_taggings_on_tag_id': needed in a foreign key constraint
/app/db/migrate/20210618092706_add_missing_unique_indices.acts_as_taggable_on_engine.rb:11:in `up'
/app/bin/rails:5:in `<top (required)>'
/app/bin/spring:15:in `<top (required)>'
/app/bin/rails:2:in `load'
/app/bin/rails:2:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Only the first migration is up. Looks like the auto generated migrations don't play well together.

kassi avatar Jun 18 '21 09:06 kassi

Got the same issue here, any solution for this?

yusupss avatar Aug 09 '21 02:08 yusupss

Can confirm this error in acts-as-taggable-on 7.0, ruby 2.7.1 and rails 6.1.4

carloseam94 avatar Sep 03 '21 15:09 carloseam94

Found in 9.0.1, rails 7.0.4, ruby 3.1.1p18

davidlbean avatar Dec 13 '22 21:12 davidlbean

same here, workaround?

bin/rails db:rollback

Commented out line in the 2nd migration as so:

#remove_index ActsAsTaggableOn.taggings_table, :tag_id if index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)

bin/rails db:migration

This allowed the db:migrate to complete. Not sure on implications.

recipedude avatar May 06 '23 19:05 recipedude