2017-06-04 31 views
0

所以我試圖在表userslooking_for_options之間創建連接表。Rails創建連接表索引名稱太長

這是我的移民文件:

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 

但我發現了這個錯誤:

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

知道,對於一個join tablerails約定Table_A_Name_Table_B_Name及其列遵循類似的約定Table_A_idTable_B_id

如何爲joint table指定較短的列名稱,以便它不會中斷rails多對多關聯?

更新:

我發現我可以給剛指數不同的名稱來代替。但rails的多對多協會究竟會利用它嗎?

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 

回答

0

... will rails's many-to-many association actually utilize it?

是否使用索引的選擇與否是由數據庫優化器作出的,其不受軌。您可以在數據庫施加的限制內將其命名爲您喜歡的內容。