【发布时间】:2022-11-10 04:01:39
【问题描述】:
我正在使用gem 'acts-as-taggable-on', '~> 9.0' 运行 Rails 7.0.0 和 Ruby 3.0.3。通过此设置和全新的 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 上的此问题:
【问题讨论】:
-
新鲜的 Rails 应用程序,但数据库呢?
-
@muistooshort 我正在运行
rails new myapp --database=postgresql命令来生成新的rails 应用程序。 -
但是您是创建了一个新数据库还是使用了已有的数据库?或者您的数据库中是否有另一个表具有名为
index_taggings_on_tag_id的索引?运行rails db,然后运行\d index_taggings_on_tag_id来查看。 -
运行后
rails db然后\d index_taggings_on_tag_id我得到Did not find any relation named "index_taggings_on_tag_id". -
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以查看索引是否存在。
标签: ruby-on-rails postgresql acts-as-taggable-on ruby-on-rails-7