gpt4 book ai didi

ruby-on-rails - Rails 创建连接表索引名称太长

转载 作者:行者123 更新时间:2023-12-04 05:30:24 25 4
gpt4 key购买 nike

所以我尝试在表 userslooking_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

但是我收到了这个错误:

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

知道对于联接表rails 约定是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

最佳答案

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

是否使用索引是由数据库优化器决定的,不受Rails的影响。您可以在数据库施加的限制范围内随意命名。

关于ruby-on-rails - Rails 创建连接表索引名称太长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44352120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com