How To Recover or undo migration in Django

What is Django Migrations?

Undo migration in Django ?, If you don’t like reading Django documentations of what migration and data base is all about then below is the step to revert a migration you have before.

Remember in django you can revert to any migrations you want but have it mind that you will lost some data you have saved earlier on. We strongly recommend backing up your data base before working on it in case any error should  occur.

Steps  to revert or reverse last migration in django

  • Run python manage.py showmigrations appname
  • Choose the migrations number you want
  • Make migrations by running python manage.py migrate appname 0042
  • Run normal migration to see if your tree is clean then 
  • delete the unwanted migration
  • re-run migration again

Managing Database Schema Changes

Django migrations is defined  as a powerful feature that enables you to manage and apply changes to your database schema over time.

When your apps data model changes such as  additions, modifications, or deletions of fields, tables, and relationships,  with migrations it provide you a systematic and automated way to keep your database schema in sync with your codebase.

The Key Concepts of Migrations are

1.Migration Files:

Migrations are represented by Python files in your apps migrations directory.

Each migration you make the  file contains a set of operations that describe the changes to the database schema. And python keep in track both the old anf new ones 

2. Migration History:

Django make sure that your previous migrattions are in track and  maintains a history of applied migrations in the database. with this Lossing your data seems imposible

This history ensures that your database schema evolves consistently across all instances of your application.

3.     Applying Migrations:

Use the makemigrations command to create new migration files based on changes in your models. Use the migrate command to apply these migrations and update the database schema accordingly. The commands are
 

python3 manage.py makemigrations
python manage'py migrate
#or 
python manage.py makemigrations
python manage.py migrate
#or you can actually run
.manage.py makemigrations
.manage.py migrate

How to recover a lost migrations 

Assuming you have model with somefields then you edited or added a model or field(s).

This will require you to run some migrations then along tye process you may encounter an error.

Note that the error is not solvable to maintain your code according to your level without employing another developer you have to reverse the old migration you have done before.

Illustration of how to reverse old migration in django

Go to your project then navigate into your app then you will see a folder name migrations,open it  when you open it you will see many migrations you have made since you started the project.

Another way to see your migrations is to use the command code .

python manage.py showmigrations yourappname
#let assume your app is Blogapp
python manage.py showmigrations Blogapp

This will display all your migrations.

Before you reverse the migrations there are some migrations that are holding migrations then to see them lets use fake migrations to see them.

python manage.py migrate Blogapp --fake

Again if you want to fake only one migration then you can do it like this.

python manage.py migrate myapp 0012 --fake

in the above migration 0012 is the name of the migrations .

Pictures of some migrations i have earlier is below.

undo migration in django

Now you have seen the image showing some of my migrations. Now we can recover it by the name or using only the code e.g 0046.

Steps required to reverse or recover a migration

  • Restructure your model to fit to an earlier migration you want to reverse to or recover or undo
  • Copy the migration name or number
  • Run the migrations to reverse the migration
  • Delete the old migrations you are done 


Let see how we can undo or reverse a migrations.

To undo migration in Django, lets assume that my model is clean with the migration i want to undo then I already know my migration i did earlier say in the above image I did a migration to alter my slug but seems that everything ia not working as i expected , but the migration name is 0046 but i want to reverse it then i will do migration going to the 0045.

As i have mentioned earlier you can apply migration with name but using the number is more covenient.

Then lets run a migration to go back to an earlier stage.

python manage.py migrate [app_name] [migration_name]


As i have provided before , we did 0046 migrations then we want to go back then we just have ti rerun 0045 lets go and migrate

python manage.py migrate blogapp 0045

here blogapp is the nane of my app while 0045 is the migrations i want to go back to.

After migrations you just have to delete the unwanted migrations.

But i recommend to always backup your code to github with your database.

To backup your data base you can follow this tutorial

How to backup and restore mysql database in django

Before running any migrations backup your code and database. this will save you a lot of stress and worry.

After deleting the last migrations you don’t want , run normal migration again.

example python manage.py migrate. and see if your tree are clean that is how to fix how to undo migration in Django.

You can also read how to install social engineering in nethunter

Leave a Reply

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