21

It looks like rolling back to any migration is possible using:

./manage.py migrate <app> <migration_to_go_to>

However, this requires an actual migration to be applied, and I need to rollback the very first migration!

Specifically, I want to rollback the built-in "sessions" app (which only has one migration, "0001_initial".)

Is there a way to achieve this?

Thanks!

Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
moomima
  • 1,200
  • 9
  • 12
  • I think you are right. Because I thought of the question in different terms ("unapply" instead of "migrate back", "very first" instead of "initial") I didn't see that question. – moomima Nov 27 '14 at 09:41

1 Answers1

49

Just do:

./manage.py migrate <app> zero

seddonym
  • 16,304
  • 6
  • 66
  • 71
  • 1
    This indeed dropped all tables and migration history, but it did not delete my `0001_initial.py` migration file. Could you please confirm whether I should manually delete it or not? – sc28 Feb 20 '19 at 14:54
  • It kind of depends on what you are trying to achieve with a backwards migration. You may benefit from a deeper understanding of the migration system before running backwards migrations, as they can get you in a mess if used wrongly! There are two migration commands. `makemigrations` will look at your models, compare them with any migration files, and create any necessary migration files that you need. It doesn't interact with the database. On the other hand, `migrate` runs commands on your database based on whatever migration files are there. It won't add/delete migration files. – seddonym Feb 21 '19 at 08:10
  • 7
    It is bonkers that it's `zero` and not `0000` – nwself May 29 '19 at 18:05