liveBook · Manning

Migration squashing

As explained in Django in Action

squashmigrations is a Django management command that combines a specified range of an app's migrations into a new migration file.

Migration squashing combines several Django migration stages into one file. It reduces the work required when repeatedly creating new project instances from scratch, while preserving the migration history needed by existing instances. section 10.3

Think of it as replacing a long sequence of instructions with one bundled instruction. Django uses the squashed migration for new installations, while older migration files can remain temporarily so existing databases continue to recognize their history. section 10.3 section 10.6

A practical squashing workflow looks like this:

  • Make and apply separate model changes, such as adding birth and death fields to the Promoter model. section 10.3
  • Run squashmigrations with the app name and the starting and ending migrations: python manage.py squashmigrations promoters 0006 0007. section 10.3
  • Commit the new migration and leave the originals in place until every deployed instance has applied the migrations being replaced. section 10.6

Promoter migration cleanup

After 0006_promoter_birth.py and 0007_promoter_death.py exist, Django creates 0006_promoter_birth_squashed_0007_promoter_death.py. The old files may remain during the transition; Django ignores them after the squashed migration replaces their dependency path.

How Django in Action covers it

How deep it goes
Explains the command, arguments, generated file, retention policy, and deployment timing, but not squashed-file internals.
Examples
Uses Promoter birth and death fields, migration files 0006 and 0007, a terminal command, and directory listings.
What it assumes
Assumes an app already has sequential migrations produced by separate model changes.
What you can do after
Apply it via Django's squashmigrations command to consolidate a selected migration range safely.
How it connects
Connects migration files with Django management commands, model changes, new installations, existing databases, and dependency chains.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.