3

I am running Rails 7.0.0 and Ruby 3.0.3 with gem 'acts-as-taggable-on', '~> 9.0'. With this setup and a fresh, new rails app installation, I am receiving the errors noted below after running rails db:migrate.

name@iMac project % rails db:migrate
== 20220105163513 ActsAsTaggableOnMigration: migrating ========================
-- create_table(:tags)
-> 0.0130s
-- create_table(:taggings)
-> 0.0085s
-- add_index(:taggings, :tag_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR: relation "index_taggings_on_tag_id" already exists
/Users/name/here/project/db/migrate/20220105163513_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:26:in `up'

Caused by:
ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR: relation "index_taggings_on_tag_id" already exists
/Users/name/here/project/db/migrate/20220105163513_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:26:in `up'

Caused by:
PG::DuplicateTable: ERROR: relation "index_taggings_on_tag_id" already exists
/Users/name/here/project/db/migrate/20220105163513_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:26:in `up'

I've ran this multiple times with fresh, new rails apps but the same error continues over and over again.

Here's what's located in /Users/name/here/project/db/migrate/20220105163513_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:26

'class ActsAsTaggableOnMigration < ActiveRecord::Migration[6.0]
  def self.up
    create_table ActsAsTaggableOn.tags_table do |t|
      t.string :name
      t.timestamps
    end

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

      t.references :taggable, polymorphic: true
      t.references :tagger, polymorphic: true

      t.string :context, limit: 128

      t.datetime :created_at
    end

    add_index ActsAsTaggableOn.taggings_table, :tag_id
    add_index ActsAsTaggableOn.taggings_table, %i[taggable_id taggable_type context],
              name: 'taggings_taggable_context_idx'
  end

  def self.down
    drop_table ActsAsTaggableOn.taggings_table
    drop_table ActsAsTaggableOn.tags_table
  end
end` 

Also see this issue on GitHub:

https://github.com/mbleigh/acts-as-taggable-on/issues/1071

mu is too short
  • 426,620
  • 70
  • 833
  • 800
jgrant
  • 582
  • 6
  • 20
  • Fresh Rails app but what about the database? – mu is too short Jan 05 '22 at 20:52
  • @muistooshort I'm running `rails new myapp --database=postgresql` command to generate the fresh rails app. – jgrant Jan 05 '22 at 21:49
  • But did you create a new database or are you using one you already had? Or is there another table in your database that has an index called `index_taggings_on_tag_id`? Run `rails db` and then `\d index_taggings_on_tag_id` to see. – mu is too short Jan 05 '22 at 22:10
  • after running`rails db` then `\d index_taggings_on_tag_id` I get `Did not find any relation named "index_taggings_on_tag_id".` – jgrant Jan 05 '22 at 22:26
  • Hmm. What's in `db/migrate/20220105163513_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb`? Anything in `db/schema.rb` related to tags? – mu is too short Jan 05 '22 at 22:58
  • @muistooshort I just added the code in `db/migrate/20220105163513_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb` and I don't have a schema.rb yet because `rake db:migrate` did not succeed. – jgrant Jan 05 '22 at 23:18
  • 2
    `t.references :tag, foreign_key: { to_table: ActsAsTaggableOn.tags_table }` should create the index that `add_index ActsAsTaggableOn.taggings_table, :tag_id` is trying to add but that should have shown up in acts-as-taggable long ago. Try commenting out the `add_index ActsAsTaggableOn.taggings_table, :tag_id` line, rerunning the migration, and the do the `\d` thing again to see if the index is there. – mu is too short Jan 06 '22 at 00:03
  • commenting out `add_index ActsAsTaggableOn.taggings_table, :tag_id` and rerunning the migration and the \d thing on database gave `alt_development=# \d index_taggings_on_tag_id Index "public.index_taggings_on_tag_id" Column | Type | Key? | Definition --------+--------+------+------------ tag_id | bigint | yes | tag_id btree, for table "public.taggings"` @muistooshort – jgrant Jan 06 '22 at 00:08
  • So (as I just noted in the github issue), looks like the acts-as-taggable is generating a broken migration, I have to be missing something obvious, I don't see how this could slip by, – mu is too short Jan 06 '22 at 01:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240812/discussion-between-jgrant-and-mu-is-too-short). – jgrant Jan 07 '22 at 05:18

1 Answers1

0

acts-as-taggable-on 9.0.1 fixes this https://github.com/mbleigh/acts-as-taggable-on/compare/v9.0.0..v9.0.1

jprosevear
  • 277
  • 5
  • 10