4

I'm building an iOS app for test taking and I want to be sure of my model before proceeding.

I found this post very helpful:

What mysql database tables and relationships would support a Q&A survey with conditional questions?

I just wanted some advice on implementing a simplified version for Core Data.

Here are some of my assumptions:

  • Each User can take one test
  • Each User has one set of answers per Test taken
  • Each Test has one User
  • Each Question has many answers

Here is my Core Data model:

enter image description here

Questions:

  • Does this model reflect my assumptions accurately?
  • Are Test_Questions and User_Answers necessary?
    • I could, in theory, have a relationship directly between Test andQuestion? Test -->> Question
  • Any advice on improvements would be very appreciated.
Community
  • 1
  • 1
Theodore Haden
  • 253
  • 2
  • 10

3 Answers3

2

You should be able to remove the TEST_QUESTIONS and USER_ANSWERS tables. There is currently a one-to-one mapping between TEST_QUESTIONS and TEST, and USER_ANSWERS and USER that really serves no purpose.

In theory, you could also combine TEST and USER since they are also one-to-one, but one could argue that they are separate concerns and should each have a table.

Also, while the design does conform to your assumption of a test having a single user, are you sure that is what you want? That means that a test could only be associated to a single user.

If that's the case, you could make another simplification. You could add a field to the ANSWER table such as "wasSelectedByUser" and remove the relationship between USER and ANSWERS since you would then be able to navigate from USER to TEST to QUESTION to ANSWER and determine what the user selected.

Possible simplification

picciano
  • 22,341
  • 9
  • 69
  • 82
0

You can use the model you defined, but it might be a little tricky when it comes to the validation phase, to know if the user has passed the test, so I suggest you make the following simplifications (i didn't take into account fields, but only the entities to make it quickly) :

Another model

With this model, when your user validate his test, you can just go with one iteration like this :

for(Answer* a in answers)
{
   Question *q = a.question;

   if(a.isCorrect)
   {  
      // ... increment counter by questions ..
      // ... check with corresponding question by using variable 'q'
   }
}

I suggest you add a new entity Category which might be useful to display a form to the user in the UI (like with a UITableView for instance). With this the datasource of your table will categories' list, your section's title will be your category's name, and your cells will be the questions.

cdescours
  • 6,004
  • 3
  • 24
  • 30
0

The model you propose does reflect your assumptions, though as others have pointed out, Test_questions and User_Answers are unnecessary since they map one-one to Test and User respectively.

But (again, as others have said), I would reconsider whether you want only one test per user and only one user per test. Indeed, your assumptions include "Each User has one set of answers per Test taken" which implies that there could be more than one test taken. To accommodate this, I would add an intermediate entity, Attempt, which will record details of the user, the test, the date, etc. By having one-many relationships from User to Attempt, and from Test to Attempt, you can allow both a) several users to take the same test, and b) any user to have several attempts, either trying different tests or trying the same test several times. I would also add a testName attribute to Test, and a questionTitle attribute to Question, for use in tableViewCells, etc.

Though not in your assumptions, your model currently requires that any one question can appear on only one test. I would suggest changing this: you could then generate different tests by randomly selecting from a bank of questions. But this does necessitate another entity, "TestQuestionDetails", which has one-many relationships from Test and Question, which could record details such as a question number. Again, as others have suggested, I would convert your category attribute on Question to a separate entity. That will allow you to add an attribute such as "sortIndex" so that you can sort categories as you wish (which you might prefer not be alphabetical).

The end result looks like this:

DataModel

pbasdf
  • 21,386
  • 4
  • 43
  • 75