How to persist branching logic into database?

后端 未结 1 571
南笙
南笙 2021-02-15 16:25

We are building a survey engine for our internal use. I would like to know how to persist the question branching logic into the database? Any body done this before or any ideas

1条回答
  •  日久生厌
    2021-02-15 16:49

    You're essentially looking to persist a decision tree into a database. You'd want to store each question as a node and, in the interests of a normalized database, store the edges in a separate table relating questions that depend on other questions (directed edges), and walk as appropriate.

    Edit: A simple design can be two tables: Questions and Edges. Questions just has id and question text. Edges can be answered_question_id, next_question_id, and answer. The first table is self-explanatory. The second table lists that if a question answered_question_id is asked and answered with something that either equals to or matches answer, forward to question next_question_id next.

    0 讨论(0)
提交回复
热议问题