[英]What's the best way to design multiple 1 to 1 relation within Postgre
I would like to know if there's a better way to design this kind of relationships within Postgre: 我想知道在Postgre中是否有更好的方法来设计这种关系:
Basically, I've got some entities that are either an EntityA
, an EntityB
or an EntityC
. 基本上,我有一些实体是EntityA
, EntityB
或EntityC
。 I will have plenty of other relationships between entities and other stuff (like Comment
in this case) so I've got this generic Entity
that defines which type of Entity
it's linked to. 我将在实体和其他东西之间有很多其他关系(例如本例中的Comment
),因此我有一个通用的Entity
,它定义了链接到的Entity
的类型。
As an example, if we have an EntityA linked to a comment, the Entity entry will look like: type: EntityA entity_a_id: x entity_b_id: null entity_c_id: null
例如,如果我们有一个链接到评论的EntityA,则Entity条目将看起来像: type: EntityA entity_a_id: x entity_b_id: null entity_c_id: null
Is there a better way to design this? 有没有更好的方法来设计这个?
This seems a good fit for the Table Inheritance feature 这似乎很适合表继承功能
CREATE TABLE entity (
-- columns that are shared among all entities
);
CREATE TABLE entity_a (
-- columns that are unique for this entity type
) INHERITS (entity);
CREATE TABLE entity_b (
-- columns that are unique for this entity type
) INHERITS (entity);
This way, entity types do not include columns of other types 这样,实体类型不包括其他类型的列
How different are the entities? 实体有何不同?
Why not just 为什么不只是
CREATE TABLE Entity
(
EntityId int,
EntityType enum(A, B, C),
FieldValue varchar(255)
)
or if the field values are significantly different and don't make sense in a single column, 或者如果字段值明显不同并且在单列中没有意义,
CREATE TABLE Entity
(
EntityId int,
EntityType enum(A, B, C),
FieldValueX varchar(255),
FieldValueY varchar(255),
FieldValueZ varchar(255)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.