I have tables:
Articles{...}
Recipes{...}
Notifications{...}
Photos{...}
And I need to implement ‘user comments’ feature (like facebook).
Should I make tables: ArticleComments, RecipesComments etc. with 1:n relationship?
Or create one Comments table for all (but I have no idea how to design this)?
You could create another table
CommentableEntity(although call it something better). Each of the rows in your tables (Articles,Recipesetc.) would have a reference to a unique row in this table. The entity table might have atypefield to indicate the type of entity (to aid reverse joining).You can then have a
Commenttable that referencesCommentableEntity, in a generic fashion.So for example you’ll end up with the following tables:
You can add the CommentableEntity record every time you add an Article/Recipe etc. All your comment-handling code has to know is the CommentableEntity_id – it doesn’t care what type of thing it is.