问题描述
我正在尝试添加一个从四个关联表的外键创建的唯一索引:
I am trying to add an unique index that gets created from the foreign keys of four associated tables:
add_index :studies,
["user_id", "university_id", "subject_name_id", "subject_type_id"],
:unique => true
数据库对索引名的限制导致迁移失败.这是错误消息:
The database’s limitation for the index name causes the migration to fail. Here’s the error message:
表 'studies' 上的索引名称 'index_studies_on_user_id_and_university_id_and_subject_name_id_and_subject_type_id' 太长;限制为 64 个字符
我该如何处理?我可以指定不同的索引名称吗?
How can I handle this? Can I specify a different index name?
推荐答案
为 ,例如:
Provide the :name
option to add_index
, e.g.:
add_index :studies,
["user_id", "university_id", "subject_name_id", "subject_type_id"],
unique: true,
name: 'my_index'
如果在 :index 选项 rel="noreferrer">references
在 create_table
块中,它采用与 add_index
相同的选项哈希作为其值:
If using the :index
option on references
in a create_table
block, it takes the same options hash as add_index
as its value:
t.references :long_name, index: { name: :my_index }
这篇关于如何在 Ruby on Rails ActiveRecord 迁移中处理过长的索引名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!