So I've been successfully creating join tables by using the name parameter when adding an index, but I'm not sure why this isn't working when I'm trying to do create a new migration:
class CreateVMailCampaignScheduleHours < ActiveRecord::Migration[5.1]
def change
create_table :v_mail_campaign_schedule_hours do |t|
t.belongs_to :v_mail_campaign_schedule, foreign_key: true
t.string :day
t.time :start_hours
t.time :stop_hours
t.timestamps
end
add_index [:v_mail_campaign_schedule_hours, :v_mail_campaign_schedule_id], name: :v_mail_campaign_schedule_id
end
end
The error I get is:
ArgumentError: Index name 'index_v_mail_campaign_schedule_hours_on_v_mail_campaign_schedule_id' on table 'v_mail_campaign_schedule_hours' is too long; the limit is 64 characters
Any suggestions? I thought my add_index would do the trick, but apparently not.
You can change it to the following:
class CreateVMailCampaignScheduleHours < ActiveRecord::Migration[5.1]
def change
create_table :v_mail_campaign_schedule_hours do |t|
t.bigint :v_mail_campaign_schedule
t.string :day
t.time :start_hours
t.time :stop_hours
t.timestamps
end
add_index :v_mail_campaign_schedule_hours, :v_mail_campaign_schedule_id, name: :index_campaign_schedule_hours_on_schedule
end
end
Your approach to create the index manually is the right one. However, t.belongs_to, which is an alias for t.references, instructs the creation of both the foreign key column and the corresponding index. So Rails still tries to create the index, before reaching add_index. Using a simple t.bigint doesn't create the index.
Yeah, so as previously said, t.belongs_to will create an index.
So, I think you can still use create_join_table, but you'll just need to specify index: false on your belongsTo.
create_join_table :v_mail_campaign_schedule_hours do |t|
t.belongs_to :v_mail_campaign_schedule, foreign_key: true, index: false
t.string :day
t.time :start_hours
t.time :stop_hours
t.timestamps
t.index [:v_mail_campaign_schedule_id], name: 'v_mail_campaign_schedule_id'
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With