I have two entities, Player and Game and the relationship between them is many to many.
Both Player and the connection between Player and Game (PlayerGame) have a score
class Player(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
score = db.Column(db.Float)
name = db.Column(db.String(45), nullable=False)
class Game(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(45))
class PlayerGame(db.Model):
player_id = db.Column(db.Integer, db.ForeignKey('players.id'), primary_key=True)
game_id = db.Column(db.Integer, db.ForeignKey('games.id'), primary_key=True)
score = db.Column(db.Float)
player = db.relationship("Player", backref="games")
game = db.relationship("Game", backref="players")
I want to write a query using sqlalchemy that given a game id, returns all related players, but their score will be overwritten by score of the game
So far my query looks like this
Player.query.join(PlayerGame, and_(
PlayerGame.player_id == Player.id,
PlayerGame.game_id == game_id))\
.options(contains_eager(Player.games))
I was thinking about changing Player.score and PlayerGame.score label names, or exluding Player.score but none of the above really seemed to work
It is possible to do it after the query in python code, but ideally, I want it to be part of the query