具有单选或多选且没有正确/错误答案的问卷的模式设计
Schema Design for a questionnaire with single or multiple choices with no right / wrong answer
首先,我在这里翻了几十个与我相关的问题,但找不到我要找的东西,所以又问了一个看似重复的问题。
我有一个问卷要求,大概30个固定问题,所有用户都可以有,
- 单个 select 选项(文本或带图像的文本)
- 多个 select 选项(文本或带图像的文本)
- 自由文本
- 日期字段
没有正确或错误的答案,因为这些问题迎合了用户的喜好。我一直在研究 postgres 架构设计,它看起来像这样。
--问题Table--
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
现在,我不确定在 multiselect 的情况下如何最好地存储答案 table 中的选项。我是否保存一个选项 ID 数组?但这会使我什至无法拥有选项 table 的外键引用,并且可能会使查询变得困难,因为我需要能够查询单个用户的所有问题的问答对,这个数组不会'帮我填充他选择的选项。请帮我想出最好的存储方式。
我的模型可能看起来像这样...丰富
--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
首先在模特处剪裁。精炼将需要更多地了解应用程序的预期用途。承认:我认为这是对@RichMurnane 模型的改进。
--user table
user_id (primary key)
first_name
last_name
other user detail columns
--survey master table, so you can reuse the model for additional surveys
survey_id (primary key)
survey_name
survey_description
--question table
question_id (primary key)
question_text
description
question_type
image_id
--survey question table
survey_question_id (primary key)
survey_id (foreign key)
question_id (foreign key
--answers table
survey_question_id (primary key, foreign key)
user_id (primary key, foreign key)
answer_type
answer_text
image_id
--image table
image_id (primary key)
image_url (url/ file path to if externally stored)
image (.jpg, .png, ...)
首先,我在这里翻了几十个与我相关的问题,但找不到我要找的东西,所以又问了一个看似重复的问题。
我有一个问卷要求,大概30个固定问题,所有用户都可以有,
- 单个 select 选项(文本或带图像的文本)
- 多个 select 选项(文本或带图像的文本)
- 自由文本
- 日期字段
没有正确或错误的答案,因为这些问题迎合了用户的喜好。我一直在研究 postgres 架构设计,它看起来像这样。
--问题Table--
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
现在,我不确定在 multiselect 的情况下如何最好地存储答案 table 中的选项。我是否保存一个选项 ID 数组?但这会使我什至无法拥有选项 table 的外键引用,并且可能会使查询变得困难,因为我需要能够查询单个用户的所有问题的问答对,这个数组不会'帮我填充他选择的选项。请帮我想出最好的存储方式。
我的模型可能看起来像这样...丰富
--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
首先在模特处剪裁。精炼将需要更多地了解应用程序的预期用途。承认:我认为这是对@RichMurnane 模型的改进。
--user table
user_id (primary key)
first_name
last_name
other user detail columns
--survey master table, so you can reuse the model for additional surveys
survey_id (primary key)
survey_name
survey_description
--question table
question_id (primary key)
question_text
description
question_type
image_id
--survey question table
survey_question_id (primary key)
survey_id (foreign key)
question_id (foreign key
--answers table
survey_question_id (primary key, foreign key)
user_id (primary key, foreign key)
answer_type
answer_text
image_id
--image table
image_id (primary key)
image_url (url/ file path to if externally stored)
image (.jpg, .png, ...)