【发布时间】:2017-11-05 05:37:00
【问题描述】:
所以我试图在表 users 和 looking_for_options 之间创建一个连接表。
这是我的迁移文件:
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
但是我收到了这个错误:
表 'looking_for_options_users' 上的索引名称 'index_looking_for_options_users_on_looking_for_option_id_and_user_id' 太长;林 它是 64 个字符
知道join table、rails 的约定是Table_A_Name_Table_B_Name,其列遵循类似的约定Table_A_id 和Table_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
【问题讨论】:
标签: ruby-on-rails