How to Revert the Last Migration in Django

1 min read

In Django, migrations are used to manage changes to the database schema, such as creating or modifying tables. Sometimes, you may need to revert the last migration to undo changes that were made to the database.

Here's how you can revert the last migration in Django. 

Note - First, make sure that you have a backup of your database before making any changes

First Identify the migrations you want to revert.

python showmigrations

To revert the last migration, run the following command. 

python migrate <app_name> <migration_name>
Where is the name of the app where the migration is located, and is the name of the migration you want to revert. You can find the name of the migration in the output from the showmigrations command.

You don't actually need to use the full migration name, the number is enough, i.e.

python migrate my_app 0010
If you want to reverse all the migrations you can use zero instead of migration name. 

python migrate my_app zero

Keep in mind that not all migrations can be reversed. This happens if Django doesn't have a rule to do the reversal. For most changes that you automatically made migrations by python makemigrations, the reversal will be possible. However, custom scripts will need to have both a forward and reverse written,

In summary, reversion of migrations in Django can be done by using the migrate command with the appropriate arguments. Additionally, testing the changes in a development environment before applying them to a production environment is a always good practice.


Latest Articles

Latest from djangocentral

Capturing Query Parameters of request.get in Django

In Django, the request object contains a variety of information about the current HTTP request, including the query parameters. Query parameters are a way to pass additional information in the URL and are used to filter or sort data. The request object p…
Read more →

2 min read

Understanding related_name in Django Models

In Django, related_name is an attribute that can be used to specify the name of the reverse relation from the related model back to the model that defines the relation. It is used to specify the name of the attribute that will be used to access the relat…
Read more →

2 min read