如何处理Ruby on Rails ActiveRecord迁移中的索引名太长? [英] How do I handle too long index names in a Ruby on Rails ActiveRecord migration?
问题描述
我正在尝试添加一个从四个关联表的外键创建的唯一索引:
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个字符
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
我该如何处理?我可以指定其他索引名称吗?
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'
如果在 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迁移中的索引名太长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!