Modelling multiple-choice question and correct answer in SQLAlchemy

You could have a correct answer intersection table, such as

class CorrectAnswer(Base):
    """ Correct answer to quiz questions """

    __tablename__ = "correct_answer"
    id = Column(Integer, primary_key=True)
    question_id = Column(Integer, ForeignKey(""))
    answer_id = Column(Integer, ForeignKey(""))
    question = relationship("Question", back_populates="correct_answer")
    __table_args__ = (UniqueConstraint("question_id", "answer_id"),)

The constraint is if you want to assume that every question has at most one correct answer.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top