OneBite.Dev - Coding blog in a bite size

How to revert or rollback migration in Django

Learn how to revert migrations in Django. This guide includes steps for reverting all or specific migrations and cautions to prevent data loss

Sometimes we need to undo or “revert” or “rollback” a migration that’s already been applied. Reverting a migration essentially involves stepping back through the sequence of migrations to get to a previous state. This article will guide you through the process of reverting migrations in Django.

Preparing to Revert Migrations

Before starting, make sure you have the latest version of Django installed and your virtual environment is activated.

1. Open your terminal and navigate to your Django project directory.

2. Enter `python manage.py showmigrations` to get a list of all your application migrations. The `[X]` or `[ ]` next to each migration name indicates whether the migration has been applied (`[X]`) or not (`[ ]`).

3. Identify the migration you want to revert to. All migrations after this point will be unapplied.

Remember, reverting migrations can lead to data loss for any fields or tables added in the unapplied migrations, as it will reverse the changes made to your database schema. Ensure you have a backup of your data before proceeding.

Reverting Migrations

To list all the migrations

python manage.py showmigrations

To revert migrations, use the `python manage.py migrate` command followed by the name of the app and the migration name you want to revert to.

python manage.py migrate <app_name> <migration_name>

For instance, if your app is called `blog` and you want to revert to a migration named `0023_add_publish_date`, you would run:

python manage.py migrate blog 0023_add_publish_date

This command will unapply all migrations down to and including `0023_add_publish_date`.

Reverting All Migrations

To revert all migrations of an app, use the `migrate` command followed by the app’s name and `zero`.

python manage.py migrate <app_name> zero

For example, to revert all migrations for the `blog` app, you would run:

python manage.py migrate blog zero

This will unapply all migrations for the `blog` app, effectively removing all tables associated with this app in your database.

Reverting Specific Migrations

If you want to unapply a specific migration without affecting those that came after it, you should create a new migration that undoes the changes you want to get rid of. Django doesn’t provide a built-in way to unapply a specific migration out of sequence because it could result in inconsistencies in your database schema.

Conclusion

Reverting migrations in Django can be a crucial task in managing your database schema as your Django application grows. As always, be cautious when handling migrations, as mistakes can lead to data loss. Always ensure you have a recent backup of your data, and thoroughly test all migrations in a development environment before applying them in production.

About Django Migration

Django, a Python-based web framework, offers an intuitive system for handling database schema migrations. As you develop your application, you’ll make changes to your database models. Django provides tools for turning these model changes into SQL code that can modify your database schema accordingly.

More about Django Migration

django