HABTM是否可以加入需求指数?(Does HABTM jointable needs index?)
是否有必要在HABTM连接表上添加add_index? 我使用t.belongs_to(又名t.references)。 这是代码
class CreateCohortsUsersTable < ActiveRecord::Migration def change create_table :cohorts_users, :id => false do |t| t.belongs_to :cohort t.belongs_to :user end add_index :cohorts_users, :cohort_id # Do I need this? add_index :cohorts_users, :user_id # Do I need this? end endIs it necessary to add_index on HABTM join table? I use t.belongs_to (aka t.references). Here is code
class CreateCohortsUsersTable < ActiveRecord::Migration def change create_table :cohorts_users, :id => false do |t| t.belongs_to :cohort t.belongs_to :user end add_index :cohorts_users, :cohort_id # Do I need this? add_index :cohorts_users, :user_id # Do I need this? end end最满意答案
在两列上添加唯一索引通常是个好主意:
add_index :cohorts_users, [:cohort_id, :user_id], unique: trueIt's usually a good idea to add a unique index on both columns:
add_index :cohorts_users, [:cohort_id, :user_id], unique: true更多推荐
发布评论