when to set a table without primary key on a join table
Hey I am using the following tables. My table course_enrollments references the user_id
and the course_id
. Both columns combined are set unique.
users
course_enrollments
courses
Rails sets primary keys by default. I have read some articles of people who used these kind of join tables without any pk. e.g. by defining :id => false
Is it appropriate for this kind of join table? what about people enrolling or leaving courses or the search for enrollments. Thanks for your time!
You generally see :id => false
on the migration for your join table when you want to use has_and_belongs_to_many
, since Rails really doesn't like finding an :id
primary key on these.
From the has_and_belongs_to_many
docs
The join table should not have a primary key or a model associated with it.
Both columns combined are set unique
Therefore your table already has a key. Unless you require other attributes there is no obvious need to make another key for it.
精彩评论