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 manage.py showmigrations
To revert the last migration, run the following command.
python manage.py migrate <app_name> <migration_name>
Where You don't actually need to use the full migration name, the number is enough, i.e.
python manage.py migrate my_app 0010
If you want to reverse all the migrations you can use zero instead of migration name. python manage.py 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 manage.py 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.