Overview
TheGame model represents a single Premier League match between two teams. It stores match results, team information, and scheduling details. This model is central to the library’s data structure, connecting teams, leagues, and game statistics.
Model Definition
Table Name:game
Location: premier_league/data/models/game.py:7
Fields
Unique identifier for the game. Primary key.
Foreign key reference to the home team. Links to
team.id.Foreign key reference to the away team. Links to
team.id.Foreign key reference to the league. Links to
league.id.Number of goals scored by the home team.
Number of goals scored by the away team.
Points awarded to the home team (3 for win, 1 for draw, 0 for loss).
Points awarded to the away team (3 for win, 1 for draw, 0 for loss).
Date and time when the game was played. Indexed for efficient querying.
The match week number in the season. Indexed for efficient querying.
The season identifier (e.g., “2023-24”).
Relationships
SQLAlchemy relationship to the home
Team object. Defined with foreign_keys=[home_team_id] and back-populates home_games on the Team model.SQLAlchemy relationship to the away
Team object. Defined with foreign_keys=[away_team_id] and back-populates away_games on the Team model.SQLAlchemy relationship to the
League object. Back-populates games on the League model.SQLAlchemy relationship to a list of
GameStats objects associated with this game (one for each team).Database Indexes
The Game model includes composite indexes for optimized querying:idx_game_season_week: Composite index on (season,match_week) for efficient season/week-based queriesidx_game_teams: Composite index on (home_team_id,away_team_id) for efficient team-based queries- Single-column index on
datefor chronological queries - Single-column index on
match_weekfor week-based queries
Methods
to_dict()
include_relationships(bool, optional): IfTrue, includes related team, league, and game stats data. Defaults toFalse.
dict: Dictionary containing all game data. Date fields are converted to ISO format strings.