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

Index name 'index_taggings_on_tag_id' on table 'taggings' already exists #845

Open
stanko-tomic opened this issue Jul 8, 2017 · 7 comments

Comments

@stanko-tomic
Copy link

When i try running rake db:migrate it outputs this
`C:\Sites\novosti>rails db:migrate
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:2:in <top (required)>' bin/rails:4:in require'
bin/rails:4:in `

'
StandardError: Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:2:in <top (required)>' bin/rails:4:in require'
bin/rails:4:in <main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)

I fixed it buy putting [5.0] on the end of the generated acts_as_taggable_on_migration.acts_as_taggable_on_engine

But then i got this error

`C:\Sites\novosti>rake db:migrate
== 20170708144217 ActsAsTaggableOnMigration: migrating ========================
-- adapter_name()
-> 0.0000s
-- adapter_name()
-> 0.0000s
-- create_table(:tags, {:id=>:integer})
-> 0.0029s
-- adapter_name()
-> 0.0000s
-- adapter_name()
-> 0.0000s
-- create_table(:taggings, {:id=>:integer})
-> 0.0032s
-- add_index(:taggings, :tag_id)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Index name 'index_taggings_on_tag_id' on table 'taggings' already exists
C:/Sites/novosti/db/migrate/20170708144217_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:23:in up' ArgumentError: Index name 'index_taggings_on_tag_id' on table 'taggings' already exists C:/Sites/novosti/db/migrate/20170708144217_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:23:in up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)`

@RaccoonFive
Copy link

I had the same problem when I was putting [5.0] after the migration name so I tried with [4.2] instead and it worked!

@alessandroasac
Copy link

Just remove the following line from *_act_as_tatggable_on_migration.acts_as_taggable_on_engine.rb:

add_index :taggings, :tag_id

Cause:
add_index :taggings, :tag_id already add the index.

@liangchaob
Copy link

liangchaob commented Aug 6, 2017

Thanks @RaccoonFive ,I have the same problem, use [4.2] instead works!

@stoplion
Copy link

stoplion commented Aug 19, 2017

Added a unless index_exists?(:taggings, :tag_id) postifx to add_index :taggings, :tag_id
line 23 on *_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb to fix.

@ltfschoen
Copy link

Using Rails 5.1.3, after generating migrations with rails acts_as_taggable_on_engine:install:migrations, I then tried to migrate them with bundle exec rails db:drop db:create db:migrate but it gave me error

Caused by:
StandardError: Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:

  class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]

So I fixed that error by just appending the missing [5.1] to each migration (i.e. class ActsAsTaggableOnMigration < ActiveRecord::Migration[5.1])

Then when I ran bundle exec rails db:drop db:create db:migrate again it gave me error:

== 20171104224110 ActsAsTaggableOnMigration: migrating ========================
-- create_table(:tags)
   -> 0.0049s
-- create_table(:taggings)
   -> 0.0191s
-- add_index(:taggings, :tag_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Index name 'index_taggings_on_tag_id' on table 'taggings' already exists

I fixed this by using advice from @stoplion by modifying db/migrate/20171104224110_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb by
appending unless index_exists?(:taggings, :tag_id) to the line add_index :taggings, :tag_id so it became add_index :taggings, :tag_id unless index_exists?(:taggings, :tag_id)

@rokumatsumoto
Copy link

rokumatsumoto commented Jun 28, 2019

Caused by:
ArgumentError: Index name 'index_taggings_on_taggable_id_and_taggable_type_and_context' on table 'taggings' does not exist

On *_add_missing_unique_indices.acts_as_taggable_on_engine.rb file

Change this line

remove_index :taggings, [:taggable_id, :taggable_type, :context]

to

if index_exists? :taggings, [:taggable_id, :taggable_type, :context]
      remove_index :taggings, [:taggable_id, :taggable_type, :context]
end

You can reference my gist for migration errors and configuration.
https://gist.github.com/rokumatsumoto/e7225334767c68c1859e097442f647a1

@ifebrand6
Copy link

Thanks @RaccoonFive ,I have the same problem, use [4.2] instead works!

thanks

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

No branches or pull requests

8 participants