Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix foreign key issue in migration #1107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sekmo
Copy link

@sekmo sekmo commented Aug 12, 2023

Hey, I've noticed that if we run the migrations generated by this gem using a MySQL database we get an error that prevents the migration from being run successfully.
Specifically, when the AddMissingUniqueIndices migration runs, we try to remove the tag_id index from the taggings table, but it fails because we have the foreign key on tags(id).

I've encountered this problem with MySQL 5.7 (the most used nowadays), 8.0.32, and 8.0.33.

This is the error:

$ rake db:migrate
== 20230812090522 ActsAsTaggableOnMigration: migrating ========================
-- create_table(:tags, {})
   -> 0.0800s
-- create_table(:taggings, {})
   -> 0.0273s
-- add_index(:taggings, [:taggable_id, :taggable_type, :context], {:name=>"taggings_taggable_context_idx"})
   -> 0.0345s
== 20230812090522 ActsAsTaggableOnMigration: migrated (0.1421s) ===============

== 20230812090523 AddMissingUniqueIndices: migrating ==========================
-- add_index(:tags, :name, {:unique=>true})
   -> 0.0268s
-- index_exists?(:taggings, :tag_id)
   -> 0.0005s
-- remove_index(:taggings, :tag_id)
rake 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
/Users/francesco.mari/workspace-offline/shop/db/migrate/20230812090523_add_missing_unique_indices.acts_as_taggable_on_engine.rb:8:in `up'

Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Cannot drop index 'index_taggings_on_tag_id': needed in a foreign key constraint
/Users/francesco.mari/workspace-offline/shop/db/migrate/20230812090523_add_missing_unique_indices.acts_as_taggable_on_engine.rb:8:in `up'

Caused by:
Mysql2::Error: Cannot drop index 'index_taggings_on_tag_id': needed in a foreign key constraint
/Users/francesco.mari/workspace-offline/shop/db/migrate/20230812090523_add_missing_unique_indices.acts_as_taggable_on_engine.rb:8:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

With this fix we remove the foreign key before the offending line, and add it again at the end, after we create the compound index.

@sekmo sekmo changed the title Fix foreign key issue migration for mysql Fix foreign key issue in migration Aug 12, 2023
@seuros
Copy link
Collaborator

seuros commented Oct 14, 2023

I think you have added those indices throught schema.rb or some other way.

Can you reproduce it in a empty database/project ?

@sekmo
Copy link
Author

sekmo commented Oct 14, 2023

mm but what's the point of trying on an empty DB? Gems are usually added to existing projects with populated DBs, no?

@seuros
Copy link
Collaborator

seuros commented Oct 15, 2023

If the tagging tables/relations existed in the database, that mean the migrations will fail.

@barnaclebarnes
Copy link

This is an issue on a new DB as well (I just ran across it). It is because the previous migration does this:

    create_table ActsAsTaggableOn.taggings_table do |t|
      t.references :tag, foreign_key: { to_table: ActsAsTaggableOn.tags_table }

Which creates a foreign key which must be removed first.

I added the line as per this PR and it fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants