Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

How to Revert the Last Migration in Django

1 min read

Table of Contents

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

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read