【问题标题】:Rails create join table index name is too longRails 创建连接表索引名称太长
【发布时间】:2017-11-05 05:37:00
【问题描述】:

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

但是我收到了这个错误:

表 'looking_for_options_users' 上的索引名称 'index_looking_for_options_users_on_looking_for_option_id_and_user_id' 太长;林 它是 64 个字符

知道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

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    ...rails 的多对多关联真的会利用它吗?

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

    【讨论】:

      猜你喜欢
      • 2012-09-21
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 2020-03-27
      • 2021-09-06
      相关资源
      最近更新 更多