首页 > 解决方案 > 带有单选或多选且没有正确/错误答案的问卷的模式设计

问题描述

首先,我在这里经历了数十个与我相关的问题,但找不到我要找的东西,因此再次问了一个看似重复的问题。

我需要一份针对所有用户的大约 30 个固定问题的问卷调查,

由于问题迎合用户的喜好,因此没有正确或错误的答案。我一直在研究 postgres 模式设计,看起来像这样。

--问题表--

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

- 选项 -

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

---答案---

  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

现在,我不确定在多选的情况下如何最好地将选项存储在答案表中。我是否保存一组选项 ID?但这会使我什至无法拥有选项表的外键引用,并且可能会使查询变得困难,因为我需要能够查询单个用户的所有问题的问答对,这个数组对我没有帮助填充他选择的选项。请帮我找出存储它的最佳方法。

标签: sqldatabasepostgresqldatabase-designschema

解决方案


我的模型可能看起来像这样......丰富

--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 

推荐阅读