【问题标题】:migrations: t.references doesn't allow index name to be specified迁移:t.references 不允许指定索引名称
【发布时间】:2015-04-27 22:22:12
【问题描述】:

我在迁移中有以下内容:

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

?

【问题讨论】:

标签: ruby-on-rails ruby


【解决方案1】:

根据Rails code for references,您可以这样做,为index 提供Hash 选项,您需要的选项称为:name,所以:

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

【讨论】:

  • 我喜欢 Rails,很简单!
【解决方案2】:

以手写方式指定它:

  t.integer :othermodel_id
  ...
end
add_index :thismodel, :othermodel_id, index_name: 'othermodel_index'

【讨论】:

    猜你喜欢
    • 2016-12-02
    • 2011-02-25
    • 1970-01-01
    • 2021-01-03
    • 2012-11-21
    • 2013-03-04
    • 2018-06-15
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多