迁移:t.references 不允许指定索引名称 [英] migrations: t.references doesn't allow index name to be specified

查看:35
本文介绍了迁移:t.references 不允许指定索引名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在迁移中有以下内容:

I have the following in a migration:

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

这会产生一个 Postgres 名称太长的索引.

That produces an index with too long of a name for Postgres.

有没有办法手动指定索引名(不用单独添加整数列和索引)?

Is there a way to manually specify the index name (without adding the integer column and the index separately)?

类似于以下内容:

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

?

推荐答案

根据references 的 Rails 代码,你可以这样做,提供 index 一个 Hash 带有选项,您需要的选项称为 :name,因此:

According to Rails code for references, you can do so, providing index a Hash with options, the one you need called :name, so:

t.references :my_field, index: { name: 'my_index_name' }

这篇关于迁移:t.references 不允许指定索引名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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