我需要为我的关系表创建代理键吗? [英] Do I need to create surrogate key for my relationship table?

查看:255
本文介绍了我需要为我的关系表创建代理键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多对多实体的关系表。



author_id | book_id



我需要添加relation_id吗?我应该能够识别使用这两个id。



谢谢

解决方案

p>由于你使用的是Doctrine,所以尽量不要在RDBMS层面上考虑太多(至少不是大部分时间)。



如果你有两个实体与ManyToMany关系,你应该忘记代理键。事实上,你应该几乎忽略关系表存在的事实。您只需要有两个相关的实体类型。



现在,如果您需要存储有关关系本身的元数据(例如徽章授予用户的日期),您超越一个简单的ManyToMany,你需要自己建模这个关系 - 通过创建一个新的实体(例如UserBadge)。那个实体当然会有一个id。



你正在使用ORM,想想实体,而不是表(大部分时间)! >

I've a relationship table for my many-to-many entities.

author_id | book_id

Do I need to add relation_id? I should be able to identify using both id.

Thank you

解决方案

Since you're using Doctrine, try not to think too much on the RDBMS level (at least, not most of the time).

If you have two Entities with a ManyToMany relationship, you should forget about the surrogate key. In fact, you should pretty much ignore the fact that the relationship table exists. You simply have two related entity types.

Now, if you need to store metadata about the relationship itself (for instance the date a badge was awarded to a user), you're going beyond a simple ManyToMany, and you need to model that relationship yourself -- by creating a new kind of entity (a UserBadge, for instance). That entity, of course, would have an id.

You're using an ORM, think about entities, not about tables (most of the time)!

这篇关于我需要为我的关系表创建代理键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆