I’m having trouble modeling a particular database structure I’m working on. To be short, considering the following:
- A webpage can have one or more threads on it
- A thread consists of one or more comments
- Comments can have one or more complaints filed against it
- Complaints can also be filed against the thread as a whole
- Complaints can also be filed against the page
I can’t quite figure out how to model this at the DB level. The first three are easy:
webpage
----------
id
name
thread
---------
id
page_id
name
comment
--------
id
thread_id
name
But if I wanted a single table of complaints, how would one model that? I don’t think you would want to do:
complaint
----------
id
page_id
thread_id
comment_id
If you ever added a new object type, like picture, you’d have to add more columns to the complaint. Is there a better way to do this, or is at as good as it gets?
Thanks in advance,
– Anthony
I would create the complaint as an entity in it’s own right, then have link table between all the different things it can be associated with.
So, I’d have the following tables …
This is a slightly different variation on Waleed’s solution. As with all things like this, there are many ways to solve it 🙂
The advantage of this approach is that you can have foreign keys to maintain data integrity. The disadvantage is that whenever you need to have complaint against a new “thing” you will need a new link table, but I suppose you’d have to create a new “thing” table anyway.