Question

In the process of learning south I've made some mistakes which I'd like to recitfy. Here's one of them:

# migrate --list
(*) 0002_auto__add_lesson #add the table
(*) 0003_auto__del_lesson #dammit, table uses wrong db engine so delete it
(*) 0004_auto__add_lesson #recreate the table

Clearly, I can just run step 4 and 2 and 3 won't be applied if they're starred:

./migrate my_app 0004

But as step 2 and 3 are redundant and don't need to be run, is it possible to simply remove them? Also, I should add Im worried someone might just run migrate which would then delete the table (and any data) and then recreate it...

Thanks in advance,

Was it helpful?

Solution

There are two considerations here.

First, if you have already run a migration, then it's recorded in database and if you just remove a file than you will run into problems: your database will refer to non-existent migration. 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. Fake means that no schema altering would be done, but notes of migrations run will be removed.

Second, you pushed you code and someone else have run your migrations. In this case you need to create a new migration that reverts anything you've done with 2 and 3. This way anyone will revert unneeded changes by running your new migration.

OTHER TIPS

Yes, it is possible but not recommended.

Step 01:

Unapply all migrations:

python3 manage.py migrate users zero --fake

Step 02: Remove specific migration records from table django_migrations.

Step 03:

Remove the actual migration file from the app/migrations directory.

Step 04: Open the next migration and refer the dependencies to the previous migration. It's more like a linked list and you want to remove a node from the middle.

Previous > Current (migration to be deleted) > Next

Done!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top