How to squash migrations in Django

To squash migrations in Django, you can use the squashmigrations management command. This command combines multiple migration files into a single file, making your database schema more concise and reducing the size of your migration history.

Here’s how you can use the squashmigrations command:

First, make sure all of your migrations have been applied to your database.

See also  How to set timezone in Django?

Run the following command:

python manage.py squashmigrations   

where is the label of the Django app you want to squash migrations for, and is the name of the last migration you want to keep. squashmigrations combines migrations from first until target (inclusive), creating single optimized file with replaces attribute linking old migrations.

The command will generate a new migration file with the squashed operations.

Apply the new migration using the following command:


python manage.py migrate  

Advanced squashmigrations options: use –squashed-name for custom names, –no-optimize to prevent operation removal, –noinput for automation.

Note: Squashmigrations production workflow: keep old migrations during deploy, migrate all servers first, commit squashed migration separately, then remove old files in second release.