How to fix any Django Migration issue in local?

How to fix any Django Migration issue in local?

Let us resolve any Django migration issue locally. Let us say you added a new field into the models.py file which caused the migration failure. The reason for migration failure can be anything, maybe a bad state of the database, or failure of old migration command, etc. I would be giving a series of processes that one can follow to fix the migration issues

Drop the Database

The fastest solution which I don't recommend but which is the easiest way, if you are starting out a project which does not have any record or no valuable record in the database is

  • Delete all the files from the migrations folders from different apps.
  • Drop the SQL database schema.
  • Run python manage.py makemigration
  • Run python manage.py migrate
Voila! Your migration issue is solved, but this also would mean that you now need to add the data to your database.

Fake old migrations

If dropping your database is not an option then the next best solution is to find the last working state of the database and then create new migration files.

  • Find the last good state where your database was working.
  • Comment out all the new models.py file changes
  • Delete all the files from the migrations folder.
  • Truncate django_migrations table
  • Run python manage.py makemigrations
  • Run python manage.py migrate --fake
  • Uncomment the changes which you did in step 2.
  • Run python manage.py makemigrations
  • Run python manage.py migrate

Now your migration is complete and the database is in a working state. Use this step only for any side project or for applications where your code won't go live to production or no other developers are working.

This is not recommended at all if you are working for production application, because for any production application you would need to commit the migration files and this process will break when the code reached your production or any other developer.

Reverse Migration

This is by far the best solution that I recommend or follow for solving any migration issue.  Most Django developers only learn about performing forward migration, they don't learn about reversing migration. Now we shall see how we can use the Django management command to perform reverse migration. Let us say we have books app, which has 4 migration files, and while performing the third migration you are seeing some unwanted migrations errors. Let us see how you can solve this problem

  • Lets us assume the error is happening after we performed some change in the database after 0002 the migration file. We know that the issue happened due to 0003 or 0004 migration step.
  • The first step would be to reverse both the migrations using Django migrate command. python manage.py migrate books 0002 would simply reverse all the migrations which were performed after 0002 step.
  • You can verify this by opening the django_migrations table.
  • Once your database is migrated to an old working state, you can now delete the 0003 and 0004 files.
  • Run python manage.py makemigrations , this would create a new migration file that would contain all the changes of the models
  • Run python manage.py migrate, this would run the new migration files change to the database.
Now your migration issue is solved. This is by far the best solution I have come across whenever I find migration issues with Django. One of the challenges you might face, is to identify the migration file which is causing the issue the migrations step or the identify the last good migration step where you can revert back
Migrations | Django documentation | Django