acts_as_taggable_on版本9.0 PG :: DuplicateTable:ERROR:关系“index_taggings_on_tag_id”已经存在。

3

我正在使用Rails 7.0.0和Ruby 3.0.3,同时使用gem 'acts-as-taggable-on', '~> 9.0'。在这个环境下,通过安装全新的Rails应用程序后运行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'

我已经多次尝试使用新的Rails应用程序运行此操作,但是相同的错误一遍又一遍地出现。
以下内容位于/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` 

请参考GitHub上的这个问题:

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


新的Rails应用程序,但数据库怎么办? - mu is too short
@muistooshort,我正在运行rails new myapp --database=postgresql命令来生成新的Rails应用程序。 - jgrant
@muistooshort,我刚刚在db/migrate/20220105163513_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb中添加了代码,因为rake db:migrate没有成功,所以我还没有schema.rb - jgrant
2
t.references :tag, foreign_key: { to_table: ActsAsTaggableOn.tags_table } 应该创建 add_index ActsAsTaggableOn.taggings_table, :tag_id 尝试添加的索引,但这应该早在 acts-as-taggable 中就出现了。尝试注释掉 add_index ActsAsTaggableOn.taggings_table, :tag_id 行,重新运行迁移,然后再次执行 \d 命令查看索引是否存在。 - mu is too short
add_index ActsAsTaggableOn.taggings_table, :tag_id 注释掉并重新运行迁移以及在数据库上执行 \d 命令,会得到以下输出: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
显示剩余5条评论
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接