请您告诉我哪里出了问题。如何索引rails中唯一的多态表
初次移徙:
class CreateAttendances < ActiveRecord::Migration
def change
create_table :attendances do |t|
t.references :attendable, polymorphic: true, index: true
t.references :user, index: true
t.timestamps null: false
end
end
end
我的当前模式:
create_table "attendances", force: :cascade do |t|
t.integer "attendable_id"
t.string "attendable_type"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "attendances", ["attendable_type", "attendable_id"], name: "index_attendances_on_attendable_type_and_attendable_id"
add_index "attendances", ["user_id"], name: "index_attendances_on_user_id"
创建了一个迁移文件
class AddUniqToAttendances < ActiveRecord::Migration
def change
add_index :attendances, [:attendable_id, :attendable_type], :unique => true
end
end
当我运行rake :migrate时,我得到了一个错误:
== 20170126154413 AddUniqToAttendances: migrating =============================
-- add_index(:attendances, [:attendable_id, :attendable_type], {:unique=>true})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::ConstraintException: UNIQUE constraint failed: attendances.attendable_id, attendances.attendable_type: CREATE UNIQUE INDEX "index_attendances_on_attendable_id_and_attendable_type" ON "attendances" ("attendable_id", "attendable_type")
我想要我在模式中的行
add_index "attendances", ["attendable_type", "attendable_id"], name: "index_attendances_on_attendable_type_and_attendable_id"
要像下面这样使用unique: true
add_index "attendances", ["attendable_type", "attendable_id"], name: "index_attendances_on_attendable_type_and_attendable_id", unique: true
发布于 2017-01-26 08:02:50
如果您的SQLite版本低于3.8.0,请尝试升级它。只有SQlite 3.8.0+支持部分索引。
https://stackoverflow.com/questions/41877556
复制