如何在Rails中解决"index unique_schema_migrations已存在"?

Jam*_*sen 4 ruby migration ruby-on-rails

运行rake db:migrate后跟rake test:units产生以下内容:

rake test:functionals
(in /projects/my_project)
rake aborted!
SQLite3::SQLException: index unique_schema_migrations already exists: CREATE UNIQUE INDEX "unique_schema_migrations" ON "ts_schema_migrations" ("version")
Run Code Online (Sandbox Code Playgroud)

相关部分db/schema.rb如下:

create_table "ts_schema_migrations", :id => false, :force => true do |t|
  t.string "version", :null => false
end

add_index "ts_schema_migrations", ["version"], :name => "unique_schema_migrations", :unique => true
Run Code Online (Sandbox Code Playgroud)

我不是在任何地方手动更改此索引,而是使用Rails的默认SQLite3适配器和全新的数据库.(也就是说,rm db/*sqlite3之前运行rake db:migrate没有帮助.)

test:units任务是否可能尝试重新加载架构?如果是这样,为什么?它不应该认识到架构已经是最新的吗?

Ian*_*nce 14

在SQLite中,索引名称唯一性在数据库级别强制执行.在MySQL中,唯一性仅在表级强制执行.这就是为什么你的迁移工作在后者而不是前者的原因:你在不同的表上有两个同名的索引.

重命名索引,或查找并重命名其他unique_schema_migrations索引,您的迁移应该有效.