Best practice for comment voting database structure

有些话、适合烂在心里 提交于 2019-12-03 07:09:39

To make sure that each voter votes only once, design your Votes table with these fields—CommentID, UserID, VoteValue. Make CommentID and UserID the primary key, which will make sure that one user gets only one vote. Then, to query the votes for a comment, do something like this:

SELECT SUM(VoteValue)
FROM Votes
WHERE CommentID = ?

Does that help?

Why don't you save the totaled votes for every comment? Increment/decrement this when a new vote has happened.

Then you have to check if the user has voted specifically for this comment to allow only one vote per comment per user.

You can put a sql join condition which returns all the votes on comments made by the current user for this object, if you get no rows, the user hasn't voted. That is just slightly different from you checking each comment one by one in the program.

as far as the database structure is concerned, keeping these things separate seems perfectly logical. vote { user_id, object_id, object_type, vote_info...)

You may be already doing this, sorry but I couldn't interpret from you post if that was the case.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!