Skip to content

Migrations: Change the database schema

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.

The commands

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.

Change a model (adding a field, deleting a model...)

When you are making a change to a model, for instance, adding a new field to: /Watcher/Watcher/data_leak/models.py Then, you need to create a new migration based on the changes you have made:

  • Go to /Watcher/Watcher/ and run this command: python3 manage.py makemigrations

[IMPORTANT] Run the makemigrations command only once, when you have made all the changes. Otherwise, it will create several unnecessary migration files.