migrations: t.references doesn't allow index name to be specified

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

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)?

Something like the following:

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

?


Solution 1:

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' }

Solution 2:

Specify it longhand:

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