I have a Quiz model that can have different types of questions, namely Numeric and Text based.
I have set up each type as a separate table, but wish to be able to access all of them by a relationship on the Quiz model.
As an extra, I would also appreciate if you could help me work out how to then make it order_by a column that all question types will have.
I have read through the sqlalchemy docs and think maybe it might have something to do with composite secondary joins?
class Quiz(db.Model):
id = db.Column(db.Integer, primary_key=True)
questions = db.relationship('Question', back_populates="centre", lazy="dynamic")
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
owner = db.relationship("User", back_populates="child")
class NumericQuestion(Question):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text, nullable=False)
quiz_id = db.Column(db.Integer, db.ForeignKey('quiz.id'), nullable=False)
quiz = db.relationship('Quiz', back_populates="questions")
answer = db.Column(db.Numeric(precision=12, scale=5))
class TextQuestion(Question):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text, nullable=False)
quiz_id = db.Column(db.Integer, db.ForeignKey('quiz.id'), nullable=False)
quiz = db.relationship('Quiz', back_populates="questions")
answer = db.Column(db.Text)