简体   繁体   English

帖子,评论,回复和点赞数据库架构

[英]Posts, comments, replies, and likes database schema

I have a site where a users can comment on posts or reply to a comment. 我有一个网站,用户可以在其中发表评论或回复评论。 The user can also like replies or comments. 用户还可以喜欢回复或评论。 However, there is another field called reply_to within the reply table. 但是,答复表中还有另一个称为reply_to的字段。 Here's my current schema: 这是我当前的模式:

Comment
id
user (foreign key)
post (foreign key)
comment

Reply
id
user (foreign key)
reply_to (who the user is replying to)
comment (foreign key)
reply

CommentLike (Table that shows which user liked which comments)
id
comment (foreign key)
user (foreign key)
like (1 = likes, 0 = dislikes)

ReplyLike (Table that shows which user liked which replies)
id
reply (foreign key)
user (foreign key)
like (1 = likes, 0 = dislikes)

Does this seem like a good schema to use, or is there a better way to create this sort of structure? 这看起来像是使用的好方案,还是有更好的方法来创建这种结构?

I would propose the structure like below having only 2 tables: 我会提出如下结构,只有2个表:

Comment:
id
user (foreign key)
post (foreign key)
comment_text
parent_comment_id (null or -1 if a new comment and comment_id of the parent if a reply)


CommentLike (Table that shows which user liked which comments):
id
comment (foreign key)
user (foreign key)
like (1 = likes, 0 = dislikes)
  • The reason to do this is because reply is nothing but a comment in itself, with only being a child to some parent comment. 这样做的原因是, reply本身只是comment ,而只是父注释的子项。 Hence, I wouldn't make it a separate entity. 因此,我不会将其设为单独的实体。
  • Note that, you will need to take care of delete operation and delete all comments who have the current comment being deleted as it's parent_id. 请注意,您将需要执行删除操作,并删除所有已删除当前注释作为其parent_id的注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM