Last Updated : 12 Jul, 2025
Django's Object-Relational Mapping (ORM) simplifies database interactions by mapping Python objects to database tables. One of the key features of Django's ORM is migrations, which allow you to manage changes to the database schema.
What are Migrations in Django?Migrations are files that store instructions about how to modify the database schema. These files help ensure that the database is in sync with the models defined in your Django project. Whenever you make changes to your models—such as adding, modifying, or deleting fields—Django uses migrations to apply these changes to the database.
Two main commands related to migrations in Django:
The makemigrations command in Django is used to generate migration files based on changes made to your models. A migration file contains Python code that describes changes to the database schema, such as creating new tables, adding fields, or altering existing fields.
How It Works:
Example:
If you add a new field to an existing model, running makemigrations will generate a migration file describing the change.
Step 2: Understanding the migrate Commandpython manage.py makemigrations
After creating migration files using makemigrations, you need to apply these changes to your database using the migrate command. This command reads all the migration files and applies the necessary database changes.
How It Works:
Example:
python manage.py migrate
After running this command, Django will update the database schema to reflect the changes described in the migration files.
Creating a Basic Django App with MigrationsNow, let’s walk through an example where we create a basic Django app, define a model, and use migrations to update the database.
Step 1: Start a New Django ProjectIn your terminal, navigate to the directory where you want to create your Django project and run the following command:
Step 2: Create a New Appdjango-admin startproject geeksforgeeks
cd geeksforgeeks
To keep the project modular, create a new app called geeks:
Step 3: Register the App in settings.pypython manage.py startapp geeks
After creating the app, you need to register it in the INSTALLED_APPS section of geeksforgeeks/settings.py:
Step 4: Define a Model in the geeks AppINSTALLED_APPS = [
...
'geeks', # Register the app here
]
Open geeks/models.py and define a simple model for our application. In this case, let's create a GeeksModel with a name and description field.
Python
from django.db import models
class GeeksModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
def __str__(self):
return self.name
Step 5: Create Migration Files
Now, let’s generate migration files for our new model:
python manage.py makemigrations
Django will create a migration file (e.g., 0001_initial.py) that includes the instructions to create the GeeksModel table in the database.
Step 6: Apply the MigrationsTo apply the migration and create the table in the database, run the following command:
python manage.py migrate
After you run makemigrations and migrate a new table would have been created in database. You can check it from geeks -> makemigrations -> 0001_initial.py.
Migration Files ExplainedOnce you run makemigrations, Django generates migration files that describe the changes made to the database schema. For example, the migration file 0001_initial.py for the GeeksModel looks like this:
Python
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name='GeeksModel',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('description', models.TextField()),
],
),
]
This file includes:
Read Next:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4