如何在Postgres表中存储喜欢的厌恶爱和愤怒?

时间:2019-11-21 16:06:55

标签: sql postgresql database-design

  • 我有典型的帖子和评论结构
  • 帖子可以喜欢不喜欢爱情和愤怒
  • 评论可以有喜欢和不喜欢的
  • 最初提出了一个非常愚蠢的模式

这其中有一个用户表,一个帖子表和一个投票表,在其中,喜欢,不喜欢,爱和愤怒存储在单独的列中

User (userId, email, password..)
Post (postId, link, summary...)
PostVotes (postVoteId, likes, dislikes, love, anger)
UserVotes (userVoteId, userId references User.userId, postVoteId references PostVotes.postVoteId)

Comments (commentId, parentCommenId, comment, publishedDate, userId)
CommentVotes (commentVoteId, likes, dislikes, commentId references Comments.commentId)
UserCommentVotes (userCommentVoteId, commentVoteId references CommentVotes, userId references User.userId)
  • 是的,我同意,这不是正确的方法
  • 在stackoverflow和dba.stackexchange.com上到处阅读几篇文章,并有个主意

而不是保留2个单独的表格来进行投票和评论投票,而是制作一个名为Votes的表格

Votes (itemId, userId, vote) 
  • 如何使itemId引用commentId和postId
  • 如果我对喜欢使用+1,对不喜欢使用-1,我该如何表示爱与愤怒?
  • 我可以使一个整数或其他东西枚举吗,如果它是一个枚举,我应该能够总结喜欢不喜欢的人的爱和愤怒,还是应该将其保留为文本枚举?
  • 我应该对每个帖子发表喜欢而不喜欢爱情和愤怒的具体视图吗?

我的查询将是在无限滚动页面中的每次加载中加载50个帖子。我需要得到喜欢每个人都不喜欢的爱和愤怒。有什么建议吗?

0 个答案:

没有答案