开发者

when to set a table without primary key on a join table

开发者 https://www.devze.com 2023-02-05 23:57 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消