how to back ref to a table that has composite key in flask-sqlalchemy?

Viewed 42

I have the following tables

class Participation(db.Model):
    """Store participant gameflow result"""
    __tablename__ = 'participation'

    participant_id = db.Column(db.Integer, db.ForeignKey(
        'participant.id'), primary_key=True)
    gameflow_id = db.Column(db.Integer, db.ForeignKey(
        'gameflow.id'), primary_key=True)

    participant = db.relationship('Participant', backref=db.backref(
        'gameflows',  lazy='dynamic', collection_class=list))
    gameflow = db.relationship('GameFlow', backref=db.backref(
        'participants',  lazy='dynamic', collection_class=list))

    def __repr__(self) -> str:
        return f"<{__class__}: participant_id: {self.participant_id}, gameflow_id: {self.gameflow_id}>"
class Result(db.Model):
    """Store participant gameflow result"""
    __tablename__ = 'result'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    participant_id = db.Column(db.Integer, db.ForeignKey(
        'participant.id'))
    gameflow_id = db.Column(db.Integer, db.ForeignKey(
        'gameflow.id'))
    result = db.Column(db.String())
    # source = db.relationship('Participation', backref=db.backref(
    #     'results',  lazy='dynamic', collection_class=list))

    def __repr__(self) -> str:
        return f"<{__class__.name}: participant_id: {self.participant_id}, gameflow_id: {self.gameflow_id}>"

Participation is a many-to-many relation of both participant and gameflow. Participation also has a 1 to many relationships with result, i.e. participants can join the same game flow more than once and have more than 1 result in the same participant_id and gameflow_id pair.

How may I link this relationship up by backref? Thank you.

0 Answers
Related