Rails create join table index name is too long

Viewed 1733

So I'm trying to create a join table between the tables users and looking_for_options.

This is my migration file:

class CreateJoinTableOptionsUsers < ActiveRecord::Migration[5.0]
  def change
    create_join_table :looking_for_options, :users do |t|
      t.index [:looking_for_option_id, :user_id]
      t.index [:user_id, :looking_for_option_id]
    end
  end
end

But I'm getting this error:

Index name 'index_looking_for_options_users_on_looking_for_option_id_and_user_id' on table 'looking_for_options_users' is too long; the lim it is 64 characters

Knowing that for a join table, rails convention is Table_A_Name_Table_B_Name and its columns follow a similar convention Table_A_id and Table_B_id.

How do I specify a shorter column name for the joint table so it doesn't break rails many-to-many associations?

Update:

I found that I can give just the index a different name instead. But will rails's many-to-many association actually utilize it?

class CreateJoinTableOptionsUsers < ActiveRecord::Migration[5.0]
  def change
    create_join_table :looking_for_options, :users do |t|
      t.index [:looking_for_option_id, :user_id], name: 'option_user'
      t.index [:user_id, :looking_for_option_id], name: 'user_option'
    end
  end
end
1 Answers
Related