如何处理Ruby on Rails ActiveRecord迁移中的索引名太长? [英] How do I handle too long index names in a Ruby on Rails ActiveRecord migration?

查看:69
本文介绍了如何处理Ruby on Rails ActiveRecord迁移中的索引名太长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一个从四个关联表的外键创建的唯一索引:

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?

推荐答案

: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆