Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolling back to a previous migration in django

I used my django migrations to migrate my database. One of the migrations was data migrations. But I made a mistake: I didn't save the model). Thus, no change was applied to data. I corrected the .py file that executed the python code for the data migrations. I now want to re-execute it. Is there a way to rollback to the previous version of my database or just running

./manage.py migrate my_app 0004 

with 0004 being the file that did the datamigrations does the trick?

like image 959
Apostolos Avatar asked Jun 10 '15 07:06

Apostolos


People also ask

How do I roll back last migration?

You can rollback your migration by using rake db:rollback with different options. The syntax will be different according to your requirements. where n is number of migrations to rollback, counting from latest migration.

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.

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.


1 Answers

In order to rollback I had to do the following

  1. showmigrations to see where I am
  2. migrate 0003 (to go one step back)
  3. showmigrations to verify that it worked and I am one version behind
  4. migrate 0004 to migrate correctly
like image 98
Apostolos Avatar answered Oct 21 '22 11:10

Apostolos