What are migrations in Django?

In Django, migration is a Python file that contains modifications to our models so that they may be translated to a database schema in our database management system. So, instead of creating queries in our DBMS shell to manually modify our database structure, we can just alter our models. Then, using Django, we can build migrations based on those model changes and run them to update our database structure.

Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They’re designed to be mostly automatic, but you’ll need to know when to make migrations, when to run them, and the common problems you might run into.

There are several commands which you will use to interact with migrations and Django’s handling of database schema:

  • migrate, which is responsible for applying and unapplying migrations.
  • makemigrations, which is responsible for creating new migrations based on the changes you have made to your models.
  • sqlmigrate, which displays the SQL statements for a migration.
  • showmigrations, which lists a project’s migrations and their status.