"""Match / scrimmage within a tryout.""" from app.extensions import db from app.models.match_model.base import BaseMatch class Match(BaseMatch): """Match / scrimmage within a tryout.""" __tablename__ = 'matches' id = db.Column(db.Integer, primary_key=True) tryout_id = db.Column(db.Integer, db.ForeignKey('tryouts.id'), nullable=False) match_type = db.Column(db.String(20), nullable=False) team1_id = db.Column(db.Integer, db.ForeignKey('teams.id'), nullable=True) team2_id = db.Column(db.Integer, db.ForeignKey('teams.id'), nullable=True) creator = db.relationship('User', backref='created_matches') tryout = db.relationship('Tryout', backref='matches') team1 = db.relationship('Team', foreign_keys=[team1_id], backref='matches_as_team1') team2 = db.relationship('Team', foreign_keys=[team2_id], backref='matches_as_team2') # delete-orphan: without it, SQLAlchemy tries to detach participants by # setting match_id to NULL, which the NOT NULL column refuses — so # deleting any match that had participants raised IntegrityError. # TeamMatch.participants already declared this; Match did not. participants = db.relationship( 'MatchParticipant', backref='match', lazy='dynamic', cascade='all, delete-orphan') def get_participating_players(self): return [p.player_id for p in self.participants.all()]