Django tips

Unapplying Migrations

Migrations don’t have to be a one-way street. In many cases, the effects of a migration can be undone by unapplying a migration. To unapply a migration, you have to call migrate with the name of the app and the name of the migration before the migration you want to unapply.

If you want to revert the migration 0002_auto_20181112_1950 in your historical_data app, you have to pass 0001_initial as an argument to the migrate command:


Naming Migrations

In the above example, Django came up with a name for the migration based on the timestamp—something like *0002_auto_20181112_1950. If you’re not happy with that, then you can use the --name parameter to provide a custom name (without the .py extension).

To try that out, you first have to remove the old migration. You have already unapplied it, so you can safely delete the file:

$ rm historical_data/migrations/

Now you can recreate it with a more descriptive name:

$ ./ makemigrations historical_data --name switch_to_decimals

This will create the same migration as before except with the new name of 0002_switch_to_decimals.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart
Scroll to Top