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

use*_*120 1 mysql schema database-design relational-database

我有一个网站,用户可以在其中对帖子发表评论或回复评论。用户还可以对回复或评论点赞。然而,回复表中还有另一个名为reply_to 的字段。这是我当前的架构:

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)
Run Code Online (Sandbox Code Playgroud)

这看起来是一个很好使用的模式,还是有更好的方法来创建这种结构?

viv*_*_23 14

我建议采用如下结构,只有 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)
Run Code Online (Sandbox Code Playgroud)
  • 这样做的原因是因为它本身reply只是一个comment孩子,只是某些父母评论的孩子。因此,我不会将其设为一个单独的实体。
  • 请注意,您需要处理删除操作,并删除当前评论被删除的所有评论,因为它是parent_id。ON DELETE CASCADE为此,您可以寻求帮助。