迁移:t.references不允许指定索引名称
我在迁移中有以下内容:
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
?
根据references
Rails代码 ,您可以这样做,为index
提供带选项的Hash
,您需要的:name
为:name
,所以:
t.references :my_field, index: { name: 'my_index_name' }
请手写指定:
t.integer :othermodel_id ... end add_index :thismodel, :othermodel_id, index_name: 'othermodel_index'