Backwards migration with Django South
Solution 1
You need to figure out the number of the migration just before the one you want to roll back.
Your app should have a migrations directory, with files in it named like
0000_initial.py
0001_added_some_fields.py
0002_added_some_more_fields.py
0003_deleted_some_stuff.py
Normally, when you run ./manage.py migrate your_app
, South runs all new migrations, in order. (It looks at the database tables to decide which ones are 'new').
However, you can also specify any migration by number, and South will migrate your database, either forward or backward, to take it to that point. So, with the example files above, if you have already migrated up to 0003, and you wanted to run 0003 in reverse (undoing it, effectively), you would run
./manage.py migrate your_app 0002
South would look at the database, realise that it has run 0003 already, and determine that it has to run the reverse migration for 0003 in order to get back to 0002.
Solution 2
Just in case someone (like me) wondered how to migrate back from initial (0001):
django-admin.py migrate some_app zero
output:
Running migrations for some_app:
- Migrating backwards to zero state.
< some_app:0001_initial
"zero" is a special state before any migration.
Reference: http://south.aeracode.org/docs/commands.html
Solution 3
Add a migration name at the end of the parameters:
./manage.py migrate app-name 00xx-migration-name
Related videos on Youtube
Ruiwen
Updated on April 07, 2020Comments
-
Ruiwen about 4 years
Ok, so this seems like a really silly thing to ask, and I'm sure I'm missing something somewhere.
How do you perform a backwards migration using South on Django?
So I've tweaked my models, created a migration with
schemamigration
, run the migration withmigrate
, and now I realise that's not quite what I wanted and I want it back the way before.Short of manually editing db tables and removing migration files, how should I go about rolling the migration back? I find references to backward migrations using South via Google, but have yet to find a solid code example for it.
Can anyone help?
-
Marshall X over 7 yearsgood question!!
-
-
mlissner almost 13 yearsUnfortunately, when you create your next migration, it doesn't skip the ones in between, so you just migrate through them again later. Seems like there could be a better way.
-
Josh Russo almost 13 years@mlissner If you really want, after rolling back the database, go to the migrations folder of the given app (in the above example your_app/migrations) and delete the unwanted migration
-
Ian Clelland almost 13 yearsExactly -- South never skips migrations; it expects that the files from 0001-nnnn represent a consistent set of migrations, for any value of nnnn. If that's not the case, then you need to re-order or delete the offending ones yourself.
-
Oliver Turner almost 12 yearsSomeone ran migrate 0001 --fake, and this was the only way to run 0001 backwards. Thanks!
-
Tomasz Gandor over 11 yearsIt's OK, and I did that before, but it's a lot of typing / pasting. The bare "state" number - in this case
00xx
- is enough. When improving and testing a migration you can have both commands in the history: forward (no argument), backward with previous state number. -
Tomasz Gandor over 10 yearsVery important answer, I wondered why
migrate 0000
didn't work. About the fake migration, yes, you may need it, if you e.g. need to only undo the (probably wrong) initial migration, but the migration history thinks, that this migration never happened.