'Where should I put this object property/attribute

I am creating a testing program. The three main objects I have right now are Tests, Questions, and Answers. I have three tables in the database, tests, questions and answers and a FK from questions to tests and a foreign key from answers to questions. As part of the questions table I have a column called correct_answer_seq_num which stores the seq_num (unique identifier) of the answer that is the correct answer to that question. I decided to put this attribute in the questions table, because only one answer can be the correct answer (for this specific test; I know there are tests where that is not the case), and if I put it in the answers table, then you could mark all answers as correct.

The trouble I am having is which object I should put this property in. Its not really an attribute of a question, it is more of an attribute of an answer, but I still think it should be in the question class for data integrity sake.

Am I making too big of a deal of this, and if not, where should I put the property?



Solution 1:[1]

I think the way you've done it in the database is correct and translates correctly into the model, too. Consider the following:

Question  AvailableAnswers  CorrectAnswer
   1 * 1           1, 2, 3              1
   1 + 1           1, 2, 3              2
   1 + 2           1, 2, 3              3

All three Questions have the same available Answers, and it's only when an Answer is related to a Question and placed amongst other Answers that you can say whether it is correct. An Answer therefore has no 'correctness' without being associated with a Question, which makes an Answer's 'correctness' a property of a Question, not an Answer. This is especially true if the same Answer could be associated with multiple Questions - correct for some, incorrect for others.

So to sum up, I think Question.CorrectAnswer makes more sense than Answer.IsCorrect.

Finally, I think it is worth making a big deal out of this sort of thing, as if you're not comfortable with your design decisions and the structure of your model it's probably a sign that something isn't right :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Wilkes