Skip to content
Advertisement

Schema Design for a questionnaire with single or multiple choices with no right / wrong answer

Firstly, I went through tens of questions over here related to mine, but couldn’t quite find what I’m looking for, hence asking a seemingly duplicate question again.

I have a requirement for a questionnaire of about 30 fixed questions for all users which can have,

  • Single select options (Text or Text with images)
  • Multi select options (Text or Text with images)
  • Free text
  • Date fields

There is no right or wrong answer since the questions cater to a user’s preferences. I’ve been working on a postgres schema design, which looks something like this.

–Questions Table–

   id int
   question varchar
   description varchar
   type varchar -> single select/multi select or free text with no options
   createdAt timestamp
   updatedAt timestamp

–Options–

  questionId int -> foreign key to questions.id
  type varchar -> text or text with image
  text varchar
  image varchar
  createdAt timestamp
  updatedAt timestamp

—Answers—

  questionId int -> foreign ref to questions.id
  option int -> foreign ref to options.id
  value varchar -> free text in case a question didn't have options
  options ? -> array of option ids? 
  userId int -> foreign ref to users.id
  createdAt timestamp
  updatedAt timestamp

Now, I’m not sure how best to store the options in the answers table in case of a multiselect. Do I save an array of option ids? but that would render me unable to even have a foreign key ref to the options table and might make querying difficult since I need to be able to query the question-answer pairs for all questions by an individual user, this array wouldn’t help me populate the options he chose. Please help me figure out the best way to store this.

Advertisement

Answer

My model would probably look something like this…Rich

--user table
user_id (primary key)
first_name 
last_name 
email 
create_date_time
update_date_time 

--survey master table, so you can reuse the model for additional surveys
survey_id (primary key)
survey_name
survey_description
create_date_time
update_date_time 

--question table
question_id (primary key)
survey_id (foreign key)
question_text
description
question_type
create_date_time
update_date_time 

--question answers table
answer_id (primary key)
question_id (foreign key)
answer_type
answer_text
answer_image
create_date_time
update_date_time 

--user answers table
user_id (primary key and foreign key)
question_id (primary key and foreign key)
answer_id (primary key if >1 answer allowed, and foreign key)
user_answer_text 
create_date_time
update_date_time 
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement