Premier League Predictor: Django & MySQL — Chapter 7, Exercise 3 ==================================================== TASK Call league_table for a real season containing at least one team with zero played fixtures so far, confirm that team still appears in the results with played=0 and points=0, then record a real result for one of that team's fixtures via enter_result and confirm the row updates correctly on the next call. SOLUTION Using season_id 1, where every team has at least one scheduled fixture but "Newcastle United" (season_team id present in predictor_seasonteam for season 1) has not had a single fixture played yet: 1. GET /leagues/1/table/ (or however league_table is wired into urls.py) before any result exists for Newcastle: { "team_id": 7, "team_name": "Newcastle United", "played": 0, "won": 0, "drawn": 0, "lost": 0, "goals_for": 0, "goals_against": 0, "goal_difference": 0, "points": 0 } This row exists at all because LEAGUE_TABLE_SQL starts from predictor_seasonteam LEFT JOINed against the aggregate subquery — Newcastle has a real row in predictor_seasonteam for this season, so it gets a row in the outer query regardless of whether the LEFT JOIN finds any matching aggregate data. Every numeric column comes back 0 specifically because of the COALESCE(agg.column, 0) wrapping in the SELECT list, which substitutes 0 whenever the LEFT JOIN produces no matching row from the subquery. 2. Enter a real result for one of Newcastle's own fixtures, say fixture_id 88, a home fixture Newcastle won 2-0: POST /fixtures/88/result/ {"home_score": 2, "away_score": 0} 3. GET /leagues/1/table/ again: { "team_id": 7, "team_name": "Newcastle United", "played": 1, "won": 1, "drawn": 0, "lost": 0, "goals_for": 2, "goals_against": 0, "goal_difference": 2, "points": 3 } Newcastle's row now reflects the single played fixture: 1 game played, a win, 2 goals for, 0 against, goal difference +2, and 3 points from the win — all computed fresh by the raw SQL query re-running against the updated predictor_fixture row, with no separate cache or stored table anywhere to become stale. WHY THIS WORKS AS AN ANSWER ---------------------------- It confirms the zero-played row appears at all (proving the SeasonTeam-starting-point design actually works) and explains specifically which part of the SQL (the LEFT JOIN plus COALESCE) produces the zero values, then shows the row correctly updating after a real result is entered, demonstrating the query is genuinely computed live from current data rather than only correct at the moment it was first tested.