I have a schema for a scorekeeping database with Game
, Team
, Player
tables.
One team has many players, each player has only one team. Each team plays many games, each game has many teams. In each game, players score a certain number points individually and as a team - this maps to a player_score
and a team_score
. A team's total score for a game is the sum of all of its players player_score
for that game and the team's team_score
for that game.
This is my plan -
GameTeam
table includes the team's team_score
for that game, and has foreign keys of Game.id
and Team.id
. Many to many.
GamePlayer
table includes the player's player_score
for that game, and has foreign keys of Game.id
and Player.id
. Many to many.
So the problem is that GameTeam
and GamePlayer
aren't linked and it seems like they should be - since a player always belongs to one team. My solution was to add a one-to-many relationship between GameTeam
and GamePlayer
, then if I have a game id and a team id I can search for a GameTeam
where those match, iterate over all the gameTeam.gamePlayers
adding each player_score
, add on the team_score
at the end, and calculate total_score
.
Does this make sense? Am I completely off? Any help appreciated, thanks. If it matters, I'm using SQLAlchemy.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…