如何用3个PrimaryKey编写文件yml多对多表
如果我们的情况是标准视图,您有两个表和一个三元,并且可以通过遵循 以下文档
但是,如果我们有三个表而不是两个表,那么三元组由 3 个 PrimaryKey 组成,正如我所写我的 yml 文件?
情况示例:
以我有一位参加课程的用户为例。 所以我有一个users
表、courses
表和一个users_has_courses
(useri_id,course_id)。 这是多对多的标准情况。 但我还有一个表“invoices”,因此还有一个表“users_courses_invoices”,其中有三个主键(user_id、course_id、invoice_id)。
if our situation is the standard view where you have two tables and a ternary, and easily manageable by following the following documentation
But if we have three instead of two tables and then the ternary consists of 3 PrimaryKey, as I write my yml files?
Example to situation:
Take the case that I have a user who participates in a course.
So I have a users
table, courses
table and a users_has_courses
(useri_id, course_id).
This is the standard case many-to-many.
But I also have a table invoices
and therefore a table users_courses_invoices
where there are three primaykey (user_id, course_id, invoice_id).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,您有
User
和Course
模型,它们与具有一对(user_id, course_id)
作为关键。我将调用此模型订阅,并为其提供自己的标识符,然后我将将此模型与Invoice
模型链接起来,因此您的最终方案(最小版本)可能是:标准化数据库,您可以使用以下代码访问用户和课程的发票:
如果您想要给定用户的所有发票,您可以通过这种方式进行查询
如果您想要给定课程的所有发票,同样适用。
In your situation you have the
User
and theCourse
model and they are linked with a many to many relation which has the pair(user_id, course_id)
as key. I would call this model subscription, and give to that its own identifier, and then I will link this model with theInvoice
model, so your final scheme (minimal version) could be:In this way you have a normalized database and you can access invoices both from users and from courses with this code:
If you want all invoices for a given users you can do a query in this way
The same apply if you want all the invoices for a given course.