How to merge consecutive database migrations in django 1.9+?

Pranjal Mittal picture Pranjal Mittal · Apr 9, 2016 · Viewed 17.9k times · Source

Migrations allow transforming from one database schema to another while maintaining current data in the database. Django allows creating migrations using the command python manage.py makemigrations

Each time makemigrations is run a new migration file 000n.. is added based on the changes detected in the models.py file.

Sometimes after making small changes to models.py, I want to run makemigrations but do not want a new migration to be created because the previous migrations haven't been used yet which allows them to be merged together, primarily because running each migration in production can take a lot of time when there is a lot of data in the database so merging migrations before hand may be preferable.

Is there a way to allow a new migration 000n.. to be merged with an existing migration 000(n-1).. ?

Answer

Abhinav picture Abhinav · Apr 9, 2016

The command you are looking for is squashmigrations. It will merge all the unapplied migrations of a given app into a single file.