在一张表中定义多个外键到多个表

发布于 2024-09-30 02:52:00 字数 462 浏览 7 评论 0原文

我有3个模型:

帖子

  • id
  • 标题
  • 正文

照片

  • id
  • 文件路径

评论

  • id
  • post_id
  • 正文

和数据库中的相应表。现在,如果我只想为我的帖子添加评论,我可以简单地添加以下外键:ALTER TABLE comment ADD FOREIGN KEY (post_id) REFERENCES post (id)。但我想对其他模特(照片、个人资料、视频等)发表评论,并将所有评论保留在一个表中。在这种情况下我该如何定义外键(我肯定需要 ORM 的外键)?

I have 3 models:

Post:

  • id
  • title
  • body

Photo:

  • id
  • filepath

Comment:

  • id
  • post_id
  • body

and corresponding tables in DB. Now, if I want to have comments only for my posts I can simply add following foreign key: ALTER TABLE comment ADD FOREIGN KEY (post_id) REFERENCES post (id). But I want to have comments for other models (photo, profile, video, etc) and keep all comments in one table. How can I define foreign keys (i definitely need FKs for ORM) in such case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自我难过 2024-10-07 02:52:00

您可以这样做:

 post:
  * post_id (PK)
  * title
  * body

 photo:
  * photo_id (PK)
  * filepath

 comment:
  * comment_id (PK)
  * body

 comment_to_post
  * comment_id (PK) -> FK to comment.comment_id
  * post_id (PK) -> FK to post.post_id

 comment_to_photo
  * comment_id (PK) -> FK to comment.comment_id
  * photo_id (PK) -> FK to photo.photo_id

仍然有可能拥有属于两个不同项目的评论。如果您认为这是一个问题,我可以尝试改进设计。

You could do this:

 post:
  * post_id (PK)
  * title
  * body

 photo:
  * photo_id (PK)
  * filepath

 comment:
  * comment_id (PK)
  * body

 comment_to_post
  * comment_id (PK) -> FK to comment.comment_id
  * post_id (PK) -> FK to post.post_id

 comment_to_photo
  * comment_id (PK) -> FK to comment.comment_id
  * photo_id (PK) -> FK to photo.photo_id

There's still the possibility of having a comment that belongs to two different items. If you think that would be an issue I can try to improve the design.

七婞 2024-10-07 02:52:00

找到一些常见的内容来发布、配置文件等——由于缺乏更好的词,我使用了实体,然后是子类型。

  • 在该模型中,一个实体可以有多个评论,一条评论仅属于一个实体。

替代文本

Find something common to post, profile, etc -- I have used Entity for a lack of better word, then subtype.

  • In this model one entity can have many comments, one comment belongs to one entity only.

alt text

恰似旧人归 2024-10-07 02:52:00

如果您想知道单个列是否可以有多个外键,那么答案是否定的,您不能。

如果需要,您可以拥有单独的外键。因此,您可以像这样修改评论表 -

 comment:
  * comment_id (PK)
  * PostID (FK to Post.PostID)
  * PhotoID (FK to <Photo>.PhotoID)
  * ProfileID (FK to <Profile>.ProfileID)
  * Body

并且,您必须确保评论表中的 PostID、PhotoID 和 ProfileID 列允许为空,并且还可能将默认值设置为空。

这是实现此目的的 DDL -

Create table Photo
(
PhotoID int,
PhotoDesc varchar(10),
Primary key (PhotoID)
)

Create table Post
(
PostID int,
PostDesc varchar(10),
Primary key (PostID)
)

Create table Profiles
(
ProfileId int,
ProfileDesc varchar(10),
Primary key (ProfileId)
)

Create table Comment  
(
CommentID int,
PhotoID int,
PostID int,
ProfileId int,
body varchar(10),
Primary key (CommentID),
Foreign key (PhotoID) references Photo(PhotoID),
Foreign key (PostID) references Post(PostID),
Foreign key (ProfileId) references Profiles(ProfileId)
)

insert into Photo values (1,'Photo1')
insert into Photo values (2,'Photo2')
insert into Photo values (3,'Photo3')

insert into Post values (11,'Post1')
insert into Post values (12,'Post2')
insert into Post values (13,'Post3')

insert into Profiles values (111,'Profiles1')
insert into Profiles values (112,'Profiles2')
insert into Profiles values (113,'Profiles3')

insert into Comment (CommentID,PhotoID,body) values (21,1,'comment1')
insert into Comment (CommentID,PhotoID,body) values (22,3,'comment2')
insert into Comment (CommentID,PostID,body) values (23,11,'comment3')
insert into Comment (CommentID,PostID,body) values (24,12,'comment4')
insert into Comment (CommentID,ProfileId,body) values (25,112,'comment5')
insert into Comment (CommentID,ProfileId,body) values (26,113,'comment6')

-- to select comments seperately for Photos, profiles and posts
select * from Comment where PhotoID is not null
select * from Comment where ProfileId is not null
select * from Comment where PostID is not null

If you want to know if you can have multiple foreign keys to a single column then the answer is no you cant.

You can have separate foreign keys if you want to. So your can modify your comment table like this -

 comment:
  * comment_id (PK)
  * PostID (FK to Post.PostID)
  * PhotoID (FK to <Photo>.PhotoID)
  * ProfileID (FK to <Profile>.ProfileID)
  * Body

And, you will have to ensure that you allow nulls in PostID,PhotoID and ProfileID columns in Comment table and also perhaps set the default value to null.

Here is the DDL to achieve this -

Create table Photo
(
PhotoID int,
PhotoDesc varchar(10),
Primary key (PhotoID)
)

Create table Post
(
PostID int,
PostDesc varchar(10),
Primary key (PostID)
)

Create table Profiles
(
ProfileId int,
ProfileDesc varchar(10),
Primary key (ProfileId)
)

Create table Comment  
(
CommentID int,
PhotoID int,
PostID int,
ProfileId int,
body varchar(10),
Primary key (CommentID),
Foreign key (PhotoID) references Photo(PhotoID),
Foreign key (PostID) references Post(PostID),
Foreign key (ProfileId) references Profiles(ProfileId)
)

insert into Photo values (1,'Photo1')
insert into Photo values (2,'Photo2')
insert into Photo values (3,'Photo3')

insert into Post values (11,'Post1')
insert into Post values (12,'Post2')
insert into Post values (13,'Post3')

insert into Profiles values (111,'Profiles1')
insert into Profiles values (112,'Profiles2')
insert into Profiles values (113,'Profiles3')

insert into Comment (CommentID,PhotoID,body) values (21,1,'comment1')
insert into Comment (CommentID,PhotoID,body) values (22,3,'comment2')
insert into Comment (CommentID,PostID,body) values (23,11,'comment3')
insert into Comment (CommentID,PostID,body) values (24,12,'comment4')
insert into Comment (CommentID,ProfileId,body) values (25,112,'comment5')
insert into Comment (CommentID,ProfileId,body) values (26,113,'comment6')

-- to select comments seperately for Photos, profiles and posts
select * from Comment where PhotoID is not null
select * from Comment where ProfileId is not null
select * from Comment where PostID is not null
又爬满兰若 2024-10-07 02:52:00

在这种情况下,您可以添加一个包含“照片”、“个人资料”的 ENUM 字段...它将是外键的第二部分

In that case you can add an ENUM field which will contain 'photo','profile'... It will be the second part of the foreign key

蓝天白云 2024-10-07 02:52:00

由于照片评论与帖子评论不同,我会将它们存储在单独的相关表中。所以我会有:

Post:

  • PostId
  • title
  • body

PostComment:

  • Commentid
  • post_id body

Photo:

  • PhotoId
  • filepath

PhotoComment:

  • Commentid
  • photo_id
  • body

使用 id 作为 PK 的名称是一种不好的做法,它使报告变得更加困难,而且更有可能在复杂查询中无意中连接到错误的表。如果您使用 tablenameID 并始终对 Fks 使用相同的名称,那么也更容易查看关系。

Since photo comments are not the same things as post comments, I would store them in separate related tables. So I would have have:

Post:

  • PostId
  • title
  • body

PostComment:

  • Commentid
  • post_id body

Photo:

  • PhotoId
  • filepath

PhotoComment:

  • Commentid
  • photo_id
  • body

It is a poor practice to use id as the name of your PK, it makes it much harder to do reporting and much more likely to inadvertently join to the wrong table in a complex query. If you use tablenameID and consistently use the same name for Fks then it is easier to see the relationships as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文