mysql Posts,comments,replies,and likes数据库模式[关闭]

b4wnujal  于 2023-10-15  发布在  Mysql
关注(0)|答案(1)|浏览(97)

已关闭,此问题为opinion-based。它目前不接受回答。
**想改善这个问题吗?**更新问题,以便editing this post可以用事实和引用来回答。

17天前关闭
Improve this question
我有一个网站,用户可以评论帖子或回复评论。用户也可以喜欢回复或评论。但是,在应答表中还有另一个名为reply_to的字段。以下是我当前的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)

这看起来像是一个很好的模式,或者有更好的方法来创建这种结构吗?

qjp7pelc

qjp7pelc1#

我建议的结构如下,只有两个表:

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)
  • 这样做的原因是因为reply本身就是一个comment,只是父注解的子注解。因此,我不会把它作为一个单独的实体。
  • 请注意,您需要注意删除操作,并删除所有将当前评论作为parent_id删除的评论。您可以使用ON DELETE CASCADE来实现这一点。

相关问题