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 Use django-adaptors

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. All migrations after will be squashed into a single migration.

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

Apply the new migration using the following command:


python manage.py migrate  

Note: Before squashing migrations, make sure you backup your database, as this process is irreversible. Also, be aware that squashing migrations can cause issues with third-party apps, so make sure to test the squashed migrations thoroughly in a test environment before applying to a production environment.