Premier League Predictor: Django & MySQL — Chapter 2, Exercise 1 ==================================================== TASK Explain why Fixture's home_team and away_team fields both need an explicit related_name, and what real error Django's own system checks would raise if one were left out. SOLUTION Django automatically creates a "reverse accessor" for every ForeignKey, letting you navigate from the related model back to the model that defines it — for example, team.fixture_set.all() would be the default reverse accessor Django generates from a plain, un-named ForeignKey on Fixture pointing at Team. That default name is derived from the model that owns the ForeignKey (Fixture), not from the field name itself. Fixture has TWO separate ForeignKey fields both pointing at Team — home_team and away_team. With no related_name specified on either, Django would try to generate the identical default reverse accessor name for both of them, since both fields point at the same target model and Django's own default naming scheme doesn't distinguish between different fields on the same source model. That's a genuine naming collision, not just an ambiguity — two different relationships both trying to claim the exact same reverse accessor name on Team. Django's own system checks (run automatically before makemigrations and migrate, or explicitly via python manage.py check) catch this and refuse to proceed, raising a real error along the lines of "Reverse accessor for 'predictor.Fixture.home_team' clashes with reverse accessor for 'predictor.Fixture.away_team'." Setting related_name='home_fixtures' on one and related_name='away_fixtures' on the other gives each ForeignKey its own distinct, explicit reverse accessor name, resolving the clash. WHY THIS WORKS AS AN ANSWER ---------------------------- It explains what a reverse accessor actually is and how Django names it by default, identifies precisely why two ForeignKeys to the same model produce a genuine naming collision rather than just an ambiguity, and names the real, specific error Django's system checks raise as a result.