Django Migrations
What are Migrations?
Migrations is a way how Django will convert your models into Database Schema. Any further or future changes in your models will be applied to your tables created in the database via migrations.
This will make your life easy to work with database which will skip the writing of SQL code.
But, here is challenge that you need to know when to make these migrations and how to make these migrations.
There are mainly 4 different commands that we will use for migrations in Django.
1. makemigrations
2. migrate
3. sqlmigrate
4. show migrations
1. makemigrations:
This command is used to create new migrations based on the changes what we make to our models. Even a small update to the model can be used to makemigrations.
Command:
python manage.py makemigrations
Usage: I just tried adding a new field in the model
(learndjango) C:\Users\admin\learndjango\lms>python manage.py makemigrations
Migrations for 'coursera':
coursera\migrations\0004_user_joined_date.py
- Add field Joined_date to user
From the above you can clearly understand that it has created the file related to migrations as coursera\migrations\0004_user_joined_date.py
Below is the file in Folder Hierarchy

Below is the code inside the file.
from django.db import migrations, models import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('coursera', '0003_auto_20200403_2357'),
]
operations = [
migrations.AddField(
model_name='user',
name='Joined_date',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
]
2. Migrate:
This command is used to apply or rollback the migrations in Django.
Command:
python manage.py migrate
Usage: Whatever the new field that was added is update with the database through migrate
(learndjango) C:\Users\admin\learndjango\lms>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, coursera, sessions
Running migrations:
Applying coursera.0004_user_joined_date… OK
3. Sqlmigrate:
This command is used to show the SQL query created for migration.
Command:
python manage.py sqlmigrate coursera 0004
Usage:
(learndjango) C:\Users\admin\learndjango\lms>python manage.py sqlmigrate coursera 0004
BEGIN;
--
-- Add field Joined_date to user
--
CREATE TABLE "new__coursera_user" ("id" integer NOT NULL PRIMARY KEY AUTOINCREME
NT, "Joined_date" datetime NOT NULL, "name" varchar(20) NULL, "email" varchar(25
4) NOT NULL, "age" integer NULL, "mobile" varchar(20) NULL);
INSERT INTO "new__coursera_user" ("id", "name", "email", "age", "mobile", "Joine
d_date") SELECT "id", "name", "email", "age", "mobile", '2020-04-04 11:36:06.168
699' FROM "coursera_user";
DROP TABLE "coursera_user";
ALTER TABLE "new__coursera_user" RENAME TO "coursera_user";
COMMIT;
4. Showmigrations:
This command is used to show all the migrations done to your application.
Command:
python manage.py showmigrations
Usage:
(learndjango) C:\Users\admin\learndjango\lms>python manage.py showmigrations
admin
[X] 0001_initial
[X] 0002_logentry_remove_auto_add
[X] 0003_logentry_add_action_flag_choices
auth
[X] 0001_initial
[X] 0002_alter_permission_name_max_length
[X] 0003_alter_user_email_max_length
[X] 0004_alter_user_username_opts
[X] 0005_alter_user_last_login_null
[X] 0006_require_contenttypes_0002
[X] 0007_alter_validators_add_error_messages
[X] 0008_alter_user_username_max_length
[X] 0009_alter_user_last_name_max_length
[X] 0010_alter_group_name_max_length
[X] 0011_update_proxy_permissions
contenttypes
[X] 0001_initial
[X] 0002_remove_content_type_name
coursera
[X] 0001_initial
[X] 0002_auto_20200403_2340
[X] 0003_auto_20200403_2357
[X] 0004_user_joined_date
sessions
[X] 0001_initial
Activate your application:
Before we apply migrations for the models, we have to activate the application that hold corresponding models. We can do that by editing settings.py file by adding the app name to the attribute “INSTALLED_APPS”.
Example:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'coursera'
]