Is there a way to undo a migration on Django and uncheck it from the list of showmigrations?

PythonDjangoMigration

Python Problem Overview


Actually, what I do is:

  • Delete the migration file.
  • Delete the row from the table of django_migrations on the database.
  • Delete the changes applied by the migration that I want to delete or unapplied.

I want to know if there's another way to do this.

Python Solutions


Solution 1 - Python

You can revert back by migrating to the previous migration. See the migrations folder of your app and then see all the migrations

Say for an example, if your migrations are something like below ordered number wise and latest migration 0012_latest_migration is applied currently.

0010_previous_migration
0011_next_migration
0012_latest_migration

And You want to go back to 0010_previous_migration

./manage.py migrate my_app 0010_previous_migration 

and then you can delete all migrations after that like here delete both 0011_next_migration and 0012_latest_migration as you already applied 0010_previous_migration.

If you're using Django 1.8+, you can show the names of all the migrations with

./manage.py showmigrations my_app

To reverse all migrations for an app to initial or start, you can run:

./manage.py migrate my_app zero

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCésar VillaseñorView Question on Stackoverflow
Solution 1 - PythonAstik AnandView Answer on Stackoverflow