我正在尝试添加一个从四个关联表的外键创建的唯一索引:
add_index :studies,
["user_id", "university_id", "subject_name_id", "subject_type_id"],
:unique => true
数据库对索引名称的限制导致迁移失败。这是错误消息:
Index name 'index_studies_on_user_id_and_university_id_and_subject_name_id_and_subject_type_id' on table 'studies' is too long; the limit is 64 characters
我该如何处理这个问题?我可以指定不同的索引名称吗?
最佳答案
向 add_index
提供 :name
选项,例如:
add_index :studies,
["user_id", "university_id", "subject_name_id", "subject_type_id"],
unique: true,
name: 'my_index'
如果在 references
上使用 :index
选项在 create_table
block 中,它采用与 add_index
相同的选项哈希作为其值:
t.references :long_name, index: { name: :my_index }
关于ruby-on-rails - 如何在 Ruby on Rails ActiveRecord 迁移中处理过长的索引名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5443740/