[RUBY] [Rails] Solution when migration error occurs in acts-as-taggable-on

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Event

When deploying with $ rails db: migrate or Capistrano An error occurs in the migration file of ʻacts-as-taggable-on` as shown below.

Terminal


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: DROP INDEX `index_taggings_on_tag_id` ON `taggings`

20200525095250/db/migrate/20200516111153_add_missing_unique_indices.acts_as_taggable_on_engine.rb:11:in `up'

Cause

It seems that the cause is that the Gem creator is simply skipping. In short, this error will continue to appear for the rest of your life unless the Gem creator modifies the Gem itself to prevent this error.

Solution

ʻActs-as-taggable-on` of the migration file created at the time of installation, Comment out unnecessary code.

** * Pay attention to the file name! ** **

ruby:20200516111153_add_missing_unique_indices.acts_as_taggable_on_engine.rb


# This migration comes from acts_as_taggable_on_engine (originally 2)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
  class AddMissingUniqueIndices < ActiveRecord::Migration[4.2]; end
else
  class AddMissingUniqueIndices < ActiveRecord::Migration; end
end
AddMissingUniqueIndices.class_eval do
  def self.up
    # add_index ActsAsTaggableOn.tags_table, :name, unique: true

    # remove_index ActsAsTaggableOn.taggings_table, :tag_id if index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
    # remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
    # add_index ActsAsTaggableOn.taggings_table,
    #           [:tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type],
    #           unique: true, name: 'taggings_idx'
  end

  def self.down
    # remove_index ActsAsTaggableOn.tags_table, :name

    # remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_idx'

    # add_index ActsAsTaggableOn.taggings_table, :tag_id unless index_exists?(ActsAsTaggableOn.taggings_table, :tag_id)
    # add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
  end
end

ruby:20200516111155_add_missing_taggable_index.acts_as_taggable_on_engine.rb


# This migration comes from acts_as_taggable_on_engine (originally 4)
if ActiveRecord.gem_version >= Gem::Version.new('5.0')
  class AddMissingTaggableIndex < ActiveRecord::Migration[4.2]; end
else
  class AddMissingTaggableIndex < ActiveRecord::Migration; end
end
AddMissingTaggableIndex.class_eval do
  def self.up
    # add_index ActsAsTaggableOn.taggings_table, [:taggable_id, :taggable_type, :context], name: 'taggings_taggable_context_idx'
  end

  def self.down
    # remove_index ActsAsTaggableOn.taggings_table, name: 'taggings_taggable_context_idx'
  end
end

Recommended Posts

[Rails] Solution when migration error occurs in acts-as-taggable-on
Error in bundle install when running rails new
Rails <% = expression%> Why no error occurs when empty
500 Internal Server Error occurs in Rails production environment
Understand migration in rails
[No Method Error] Solution when new action occurs
Migration error after Activerecord association in Rails5 + Docker environment (2)
Migration error after Activerecord association in Rails5 + Docker environment
Error in rails db: migrate
Error when using rails capybara
An error occurs when codedeploy-agent is installed in Ubuntu Server 20.04
Exit code 1 occurs when Rails is stopped in Docker environment
[Rails] Unexpected validation error in devise
[Solution] Webpacker :: Manifest :: MissingEntryError in Rails
Collecting client information when an error occurs in a web application
Solution notes when an error occurs when downloading docker gpg using curl
I don't see an error in Rails bundle install ... the solution
Rails migration
What to do and how to install when an error occurs in DXRuby 1.4.7
Error in implementation when implementing Spring validation
A reminder when an aapt.exe error occurs
[Solution] Webpacker error when introducing Vuetify to Rails6 "ActionView :: Template :: Error Webpacker can't find hello_vue ~"
Solution that gives an error when trying to connect to DB (MySQL) in Java
Dealing with NameError: uninitialized constant :: Analyzable error when installing Active Storage in Rails6
Error 400 occurs when executing Query by specifying Japanese as a condition in SOQL
When using a list in Java, java.awt.List comes out and an error occurs
[Rails] Error resolution when generating tokens with PAYJP
What to do when IllegalStateException occurs in PlayFramework
[Rails] ActiveRecord :: HasManyThrough Order Error in Users # show
Simple notification function in Rails (only when followed)
[In team development] Error when moving to another member's branch and doing rails s [Rails]
Ruby on Rails Incorrect string value error resolution when posting a form in Japanese
Group_by in Rails
Customize the display when an error such as 404 Not Found occurs in Spring Boot
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Rails] About error resolution when installing devise and activeadmin
Error encountered with notes when deploying docker on rails
[Rails] "private method` String' called ~ "error when db: migrate
What to do when a null byte error occurs
Regarding overcapacity when setting Rails tutorial environment in Cloud 9
Solution when class_cast_exception appears in create_index! Of elasticsearch-rails (7 series)
Error ExecJS :: RuntimeUnavailable: What to do when it occurs
Rails 6 startup error "Webpacker configuration file not found" Solution
Syntax error when including HTML tags in Draper + create.js.erb
[Runy On Rails] Active Hash Error NameError in Controller ActiveHash Did you mean? ActiveStorage): Solution
About the error that occurred when adding the column name in rails (rails db: migrate, rails db: rollback, add)
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]