何时在连接表上设置没有主键的表
嘿,我正在使用下表。我的表 course_enrollments 引用了 user_id
和 course_id
。组合的两列均设置为唯一。
用户
课程注册
courses
Rails 默认设置主键。我读过一些人的文章,他们使用这种类型的连接表,没有任何PK。例如通过定义 :id => false
适合这种连接表吗?人们注册或退出课程或寻找注册的情况又如何呢?感谢您抽出时间!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您通常会看到
:id =>当您想要使用
,因为 Rails 确实不喜欢在这些表上查找has_and_belongs_to_many
时,请在连接表的迁移上设置 false:id
主键。来自
has_and_belongs_to_many
文档You generally see
:id => false
on the migration for your join table when you want to usehas_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因此您的表已经有一个键。除非您需要其他属性,否则显然不需要为其创建另一个密钥。
Therefore your table already has a key. Unless you require other attributes there is no obvious need to make another key for it.