QuestionOrAnswer model?

Posted by Mark on Stack Overflow See other posts from Stack Overflow or by Mark
Published on 2010-03-20T01:08:25Z Indexed on 2010/03/20 1:11 UTC
Read the original article Hit count: 255

My site has Listings. Users can ask Questions about listings, and the author of the listing can respond with an Answer. However, the Answer might need clarification, so I've made them recursive (you can "answer" an answer).

So how do I set up the database? The way I have it now looks like this (in Django-style models):

class QuestionOrAnswer(Model):
    user = ForeignKey(User, related_name='questions')
    listing = ForeignKey(Listing, related_name='questions')
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children')
    message = TextField()

But what bugs me is that listing is now an attribute of the answers as well (it doesn't need to be). What happens if the database gets mangled and an answer belongs to a different listing than its parent question? That just doesn't make any sense. We can separate it with polymorphism:

QuestionOrAnswer
    user
    message
    created
    updated
Question(QuestionOrAnswer)
    shipment
Answer(QuestionOrAnswer)
    parent = ForeignKey(QuestionOrAnswer)

And that ought to work, but now ever question and answer is split into 2 tables. Is it worth this overhead for clearly defined models?

© Stack Overflow or respective owner

Related posts about design-decisions

Related posts about design-patterns