It is very common coming up with the situation where you have multiple tables, let's say Posts
, Entries
and News
and the users can comment in any of them.
The natural way to think this would look like would be something like this:
However, using the parent_type
attribute to set up the primary key seems ambiguous to me, because it doesn't really describe clearly to which parent the comment belongs to.
It would seem to me like the clearest way to understand the relationship would be to create an intermediate table for each relationship: posts_comments
, posts_entries
, posts_news
. But then again this doesn't have much sense in terms of database design because you don't really need an intermediate table for a one to many
relationship, they are only needed for many to many
relationships.
Then maybe a third approach would be to make a different Comment
model depending on what it belongs to: PostComment
, EntryComment
. This would help in understanding better what that comment is for, but I think it is a terrible idea because a comment could just be represented by a single table and you could end up with a bunch of different tables with repeated column while it could just be represented by one table.
So, what is the usual approach for this situation?