加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – migrations:t.references不允许指定索引名称

发布时间:2020-12-16 20:16:15 所属栏目:百科 来源:网络整理
导读:迁移中有以下几个: create_table :model_with_a_long_name do |t| t.references :other_model_with_an_equally_long_name,index: trueend 这会产生一个索引,其长度不能超过Postgres. 有没有办法手动指定索引名称(不分开添加整数列和索引)? 类似以下内容:
迁移中有以下几个:
create_table :model_with_a_long_name do |t|
  t.references :other_model_with_an_equally_long_name,index: true
end

这会产生一个索引,其长度不能超过Postgres.

有没有办法手动指定索引名称(不分开添加整数列和索引)?

类似以下内容:

create_table :model_with_a_long_name do |t|
  t.references :other_model_with_an_equally_long_name,index: true,index_name: 'model_and_other'
end

解决方法

根据 Rails code for references,您可以这样做,为索引提供一个Hash选项,您需要调用的名称:name,so:
t.references :my_field,index: { name: 'my_index_name' }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读