Django Migrations Guide
Django Migrations Guide
makemigrations
migrate
sqlmigrate
showmigrations
Django’s migration is a version control system. Whenever you add a new model or effect changes in
an existing model, you need to run the makemigrations command. It creates a script for making
changes in the mapped table. Every time you run the makemigrations command and Django
detects the changes, a script with its name and version number is created. To implement the
changes according to the migration script, you need to run the migrate command.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/8
package is also created, which is empty to begin with.
If you open the migration file, you’ll find a migration class in it.
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Person',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False,
verbose_name='ID')),
('name', models.CharField(max_length=20)),
('email', models.EmailField(max_length=254)),
('phone', models.CharField(max_length=20)),
],
),
]
As mentioned above, you need to run the migrate command to apply the tasks in the migrations file
to be performed.
(django) C:\django\myproject>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, myapp, sessions
Running migrations:
Applying myapp.0001_initial... OK
Have a look at the tables in your database db.sqlite3. The person table with three fields can be seen
in it.
Version control
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/8
Now, let's modify the person model class by changing the namefield to Person_name and running
makemigrations again.
(django) C:\django\myproject>python manage.py makemigrations
Was person.name renamed to person.person_name (a CharField)? [y/N] y
Migrations for 'myapp':
myapp\migrations\0002_rename_name_person_person_name.py
- Rename field name on person to person_name
A second migration script is created in the migrations folder. Before finalizing the change, add a
new field – age – in the person model and run makemigrations again.
(django) C:\django\myproject>python manage.py makemigrations
Migrations for 'myapp':
myapp\migrations\0003_person_age.py
- Add field age to person
Showmigrations command
Now there are two unmigrated changes in the model. Run the showmigrations command:
(django) C:\django\myproject>python manage.py showmigrations
...
...
myapp
[X] 0001_initial
[ ] 0002_rename_name_person_person_name
[ ] 0003_person_age
...
The initial migration (file numbered 0001) has already migrated. The X mark is indicative of this.
However, the next two migrations don’t show the X mark, which means they are pending. If we run
the migrate command, both modifications will be reflected in the table structure.
(django) C:\django\myproject>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, myapp, sessions
Running migrations:
Applying myapp.0002_rename_name_person_person_name... OK
Applying myapp.0003_person_age... OK
As mentioned earlier, Django’s migration mechanism provides efficient version control. You may
want to fall back upon the table structure before adding the age field. Run the migrate command
and specify which migration file to be used so that the migrations after it will be undone or
unapplied.
(django) C:\django\myproject>python manage.py migrate myapp
0002_rename_name_person_person_name
Operations to perform:
Target specific migration: 0002_rename_name_person_person_name, from myapp
Running migrations:
Rendering model states... DONE
Unapplying myapp.0003_person_age... OK
sqlmigrate Command
Lastly, the sqlmigratecommand shows the SQL query or queries executed when a certain migration
script is run. For example, the first migration over the myapp’s person model is intended to create
the person table. The sqlmigratecommand for this script shows the CREATE TABLE statement for this
purpose.
(django) C:\django\myproject>python manage.py sqlmigrate myapp 0001_initial
BEGIN;
--
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/8
-- Create model Person
--
CREATE TABLE "myapp_person" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name"
varchar(20) NOT NULL, "email" varchar(254) NOT NULL, "phone" varchar(20) NOT NULL);
COMMIT;
-> make it easy to understand
Django migrations help translate model changes into database tables and update them when needed.
This ensures that your database structure matches your models.
When you create a Django project, some apps like authentication and sessions are installed by default.
You need to apply their migrations so their database tables are created.
Run:
bash
This will set up necessary tables like users, permissions, and sessions.
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/8
Define a model in `models.py`:
python
class Person(models.Model):
name = models.CharField(max_length=20)
email = models.EmailField()
phone = models.CharField(max_length=20)
bash
You'll see:
bash
4. Applying Migrations
Run:
bash
Output:
Applying myapp.0001_initial... OK
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/8
python
class Person(models.Model):
person_name = models.CharField(max_length=20)
email = models.EmailField()
phone = models.CharField(max_length=20)
bash
css
python
age = models.IntegerField()
bash
Run:
bash
You'll see:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/8
css
myapp
[X] 0001_initial
[ ] 0002_rename_name_person_person_name
[ ] 0003_person_age
Apply them:
bash
bash
bash
Example output:
sql
Summary
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/8
Command Purpose
By using migrations, you ensure that changes in models are safely and systematically applied to your
database. 🚀
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/8