tags:

views:

27

answers:

2

in my forum i have threads and replies.

one thread has multiple replies. but then, a reply can be a reply of an reply (like google wave). because of that a reply has to have a column "reply_id" so it can point to the parent reply. but then, the "top-level" replies (the replies directly under the thread) will have no parent reply.

so how can i fix this? how should the columns be in the reply table (and thread table).

at the moment it looks like this:

threads: id title body

replies: id thread_id (all replies will belong to a thread) reply_id (here lies the problem. the top-level replies wont have a parent reply) body

what could a smart design look like to enable reply a reply?

+1  A: 

You have a heirarchical structure. There are two standard ways of storing this in a database:

This article by Quassnoi covers the pros and cons of each of these models. Your current design is using adjacency lists. That's OK, but you need to be aware that MySQL doesn't natively support recursive CTEs and that you have to use some tricks to get the data out efficiently, as described in the article.

Mark Byers
but what about my reply_id in the top-level replies? they have no parent reply, so what should the id be there?
never_had_a_name
@ajsie: In the article Quassnoi uses parent=0 to mean that something is at the top-level.
Mark Byers