Search code examples
mysqlcommentsnested-sets

nested set comments question


I'm fairly new to nested sets although I have used them before for creating a store structure. I want to use nested sets for a commenting system now but I can't wrap my head around it and there are very few examples of this.

the site is basically a blog and I want to allow people to comment on the postings and to respond to other comments.

This is my question:

is each comment a node? if so do I create a root node each time a new post is created and add the comments as child nodes to the root?

This is the only way I see this working but Im not crazy about creating a root node for every posting to me the comments table should be untouched until the first actual comment is made?

Can anyone explain this to me?


Solution

  • Nested set is used if you want to be able easly to retrieve the entire hieracy. In your case, every comment will have post_id (probably it will have in_reply_to - if you allow hiearical comments), and nested set is not needed, as you will get all comments by post_id. If you expect a lot of comments to each post, you can use the nested set for the comments, and you will have a lot of root nodes (one for each 1st level comment - a comment that is not reply to a comment, but is goind directly to the post). I don't see any issue with having a lot of root nodes, and I don't understand your consideration about 'the table should be untouched until the first actual comment'?