Thursday, October 3, 2024

makemigrations vs migrate in Django: Key Differences

Django makemigrations vs migrate – Complete Beginner to Advanced Guide

๐Ÿ Django makemigrations vs migrate – Complete Guide

Django provides two core commands for database management:

  • makemigrations – prepares changes
  • migrate – applies changes

They look similar, but they perform completely different roles in the database lifecycle.


๐Ÿ“š Table of Contents


๐Ÿง  Overview

Django separates planning changes and applying changes to the database.

This avoids direct risky changes to the database and ensures version control for schema evolution.


⚙️ What is makemigrations?

This command detects changes in models.py and creates migration files.

Command

python manage.py makemigrations

What it does internally:

  • Scans model changes
  • Compares with last migration
  • Generates Python migration scripts

Example Output

View CLI Output
Migrations for 'app':
  app/migrations/0002_add_age_field.py
    - Add field age to Student

Important Idea

It does NOT change the database. It only prepares instructions.


๐Ÿš€ What is migrate?

This command applies migration files to the database.

Command

python manage.py migrate

What it does internally:

  • Reads migration files
  • Converts them into SQL
  • Executes SQL on database

Example Output

View CLI Output
Applying app.0002_add_age_field... OK

๐Ÿ” Django Migration Workflow

Step-by-step process:

  1. Create or modify model
  2. Run makemigrations
  3. Generate migration file
  4. Run migrate
  5. Database updated

๐Ÿ“ Database Mapping (Simple Mathematical Model)

Think of Django migrations as a transformation function:

\[ Database_{new} = f(Database_{old}, Migration) \]

Explanation:

  • Database_old = current schema
  • Migration = instructions (like rules)
  • f() = transformation engine (Django ORM)
๐Ÿ‘‰ In simple words: Migration is a set of rules that transforms your old database into a new structure.

Another way to think:

\[ Schema_{t+1} = Schema_t + \Delta Changes \]

  • \( \Delta Changes \) = new fields, tables, deletions

๐Ÿ–ฅ️ CLI Example Workflow

Step 1: Create model

class Student(models.Model): name = models.CharField(max_length=100)

Step 2: Run makemigrations

python manage.py makemigrations

Step 3: Migration file created

Generated File
0001_initial.py

Step 4: Apply migration

python manage.py migrate

Step 5: Database updated


⚖️ makemigrations vs migrate

Feature makemigrations migrate
Purpose Create migration files Apply migrations to DB
Affects DB? No Yes
Output Python migration scripts SQL execution
Usage stage Development step Deployment/runtime step

๐Ÿ’ก Best Practices

  • Always run makemigrations after model changes
  • Commit migration files in Git
  • Run migrate before deploying
  • Never edit migration files manually unless necessary

๐ŸŽฏ Final Summary

makemigrations prepares changes.

migrate applies changes.

Together, they ensure safe and structured database evolution in Django applications.

No comments:

Post a Comment

Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts