Jonathan
Jonathan

Reputation: 3024

Voting system, would you have thread votes and comment votes in same table?

Creating a voting system just like stackoverflow. Questions like this have been asking several times and i have checked but i havent found one on a voting system where you are rating thread and comments. Im wondering what is the best way to do this?

Is this the best way to do it?

thread table
id PK
userid
title
description

.

comment table
id PK
userid
comment

have ratings in the tables above or have them separate like? :

thread_rating table
threadid PK
userid PK
rating

.

comment_rating table
commentid PK
userid PK
rating

or just have one rating table for comments and threads?

Upvotes: 1

Views: 142

Answers (2)

Andrew Barber
Andrew Barber

Reputation: 40150

If your Comments are in a separate table from the Threads, then the ratings for each probably have no business being in the same table. So yes; given how you intend to do the Threads and Comments, you should have separate tables for each of their ratings.

What @drachenstern mentions is also important to consider, btw; you might do something with a View or some other mechanism to track overall voting by voter (and by voted-upon) users. For instance, SO displays our total up/down votes, whether they are on questions or answers. Yet at the same time, there are badges where it's important to know which are which.

One issue which can happen if you combine the tables as a way to 'combine' the votes totals is that you then might have to have a different way to generate UIDs for the Threads and Comments, to assure that no thread ever has the same ID as a comment (which would mean you'd need to make the table structure more complex). In such a line of thinking, my opinion would probably be to combine Threads and Comments into one table. That introduces its own complications though, of course.

:)

Upvotes: 2

jcolebrand
jcolebrand

Reputation: 16035

I imagine it would make sense to track who voted for each, so a table for <user,commentid,vote,timestamp> and a table for <user,answerid,vote,timestamp>

Otherwise I agree with @AndrewBarber

Upvotes: 1

Related Questions