Mysql 发布、评论、回复和喜欢数据库架构

Mysql 发布、评论、回复和喜欢数据库架构,mysql,database-design,relational-database,schema,Mysql,Database Design,Relational Database,Schema,我有一个网站,用户可以评论帖子或回复评论。用户也可以喜欢回复或评论。但是,在reply表中还有另一个名为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 th

我有一个网站,用户可以评论帖子或回复评论。用户也可以喜欢回复或评论。但是,在reply表中还有另一个名为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)

这似乎是一个很好的模式,还是有更好的方法来创建这种类型的结构?

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

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
    本身就是一个
    注释
    ,只是某个父注释的子注释。因此,我不会让它成为一个单独的实体
  • 请注意,您将需要注意删除操作,并删除所有将当前注释作为其父注释id删除的注释

Hmm,如果我们假设
Reply
还有另一个名为
Reply\u to
的列来指示用户正在回复谁呢。例如,假设我们有两个回复行,每行都是对同一条评论的回复,但是,一个回复是对发布另一个回复的人的回复
reply\u to
将具有此人的用户id。您是否仍然建议使用您建议的相同结构,添加
reply\u to
列?@user2896120
parent\u comment\u id
是被回复的评论的id。是的,我说
reply\u to
是此人回复的用户。因此,用户正在向reply@user2896120因此,您可以获取表中具有用户id的父注释id行,并通知他们。从技术上讲,您必须通知从该级别到顶层的所有用户。我明白了。。。所以基本上
parent\u comment\u id
就是用户回复的任何注释。因此,它可以是对评论的回复,也可以是对回复的回复。在我看来,回复到字段是多余的,因为comment->user的关系是1对1。回复注释后,该注释只属于一个用户,您可以通过注释id轻松返回该用户。