Open In App

Django Basic App Model - Makemigrations and Migrate

Last Updated : 09 Oct, 2025
Comments
Improve
Suggest changes
18 Likes
Like
Report

Django’s Object-Relational Mapper (ORM) simplifies database interactions by mapping Python objects to database tables. A key feature of the ORM is migrations, which enable seamless management of changes to the database schema over time.

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 the Django project. Whenever changes are made to models, such as adding, modifying, or deleting fields, Django uses migrations to apply these changes to the database.

Commands:

  • makemigrations: creates migration files based on changes made to your models.
  • migrate: applies the generated migration files to the database.

Step-by-Step Guide to Migrations in Django

Step 1: Makemigrations Command

The makemigrations command generates migration files that describe changes to the database schema in Python code.

  • Creates new tables when new models are added.
  • Adds new fields to existing models.
  • Alters existing fields or modifies table structure.

How It Works:

When python manage.py makemigrations is run, Django detects changes in the models and generates migration files.

  • Migration files are created inside the migrations/ directory of the app.
  • Each file is automatically numbered (e.g., 0001_initial.py, 0002_auto_...) to represent incremental changes.
  • These files describe changes to the database schema in Python code, ready to be applied with migrate.

Step 2: Migrate Command

After creating migration files using makemigrations, the migrate command applies these changes to the database.

  • Reads all migration files in the project.
  • Performs necessary updates to the database schema.
  • Creates or modifies tables and fields as defined in the migration files.

How It Works:

The python manage.py migrate command applies the changes in the correct order by reading the migration files generated by makemigrations. It handles:

  • Creating tables
  • Modifying columns
  • Adding indexes
  • Performing other database-related operations

Creating a Basic Django App with Migrations

Consider a project named ‘geeksforgeeks’ having an app named ‘geeks’. After setting up the project and app, let’s define a model and use migrations to update the database accordingly.

1. In geeks/models.py:

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

2. Create Migration Files

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.

3. Apply the Migrations

To apply the migration and create the table in the database, run the following command:

python manage.py migrate

After running makemigrations and migrate, a new table is created in the database. This can be verified in geeks -> migrations -> 0001_initial.py.

Migration Files Explained

After running makemigrations, Django automatically generates migration files that describe the changes made to the database schema.

As an example, Django generates a migration file 0001_initial.py for the GeeksModel, which could look 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:

  • Operations: Defines the changes to be applied to the database (e.g., creating the GeeksModel table).
  • Dependencies: Lists other migrations this one depends on. Since this is the first migration, no dependencies are defined.

Explore