Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 259
César Villaseñor Avatar asked Apr 06 '17 23:04

César Villaseñor


People also ask

How do I remove a specific migration in Django?

There is a solution: run migrations 2 and 3 backwards with ./manage.py migrate my_app 0001 , then delete migration files. If you can't migrate back (e.g. you messed up with your database manually) then you can fake migrate back with ./manage.py migrate my_app 0001 --fake and set up database as it should be manually.

How do I undo migration?

Undoing Migrations​ With migration you can revert to old state by just running a command. You can use db:migrate:undo , this command will revert most the recent migration. You can revert back to the initial state by undoing all migrations with the db:migrate:undo:all command.


1 Answers

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 
like image 183
Astik Anand Avatar answered Sep 19 '22 22:09

Astik Anand