是否有必要在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
end
答案 0 :(得分:0)
在两列上添加唯一索引通常是个好主意:
add_index :cohorts_users, [:cohort_id, :user_id], unique: true