Migrations - potatoscript/django GitHub Wiki

Migrations in Django are step-by-step instructions that Django writes to help your database stay in sync with the changes you make in your models.py.

🧠 Why Do We Need Migrations?

Imagine you're building with LEGO 🧱
You decide to:

  • Add a new block (new model or field)
  • Remove a block (delete a model or field)
  • Rename a block (rename a model or field)

You need a way to tell your LEGO box (database) that something has changed. That's what migrations do!


πŸͺ„ Step-by-Step: Making and Applying Migrations

βœ… 1. Make a Change in Your Model

For example, let’s add a brand to our Toy model.

πŸ“„ models.py

class Toy(models.Model):
    name = models.CharField(max_length=100)
    brand = models.CharField(max_length=100, default="No Brand")
    ...

πŸ§™β€β™‚οΈ 2. Run makemigrations

In your terminal:

python manage.py makemigrations

πŸŽ‰ Django will detect the change and create a new file in migrations/.

πŸ—‚ You’ll see something like:

Migrations for 'myapp':
  myapp/migrations/0002_toy_brand.py

🧱 3. Run migrate to Apply It to the Database

python manage.py migrate

This will actually update your database structure to match the model.


πŸ“¦ Let’s Do a Full Example

Step 1: Original Model

class Toy(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=6, decimal_places=2)

Step 2: Modify the Model (Add Field)

class Toy(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    rating = models.FloatField(default=5.0)  # new!

Step 3: Make Migrations

python manage.py makemigrations

Output:

Migrations for 'myapp':
  myapp/migrations/0002_toy_rating.py
    - Add field rating to toy

Step 4: Apply Migrations

python manage.py migrate

Output:

Applying myapp.0002_toy_rating... OK

πŸŽ‰ The database now has a new rating column!


πŸ“š Extra Commands You Should Know

Command What It Does
python manage.py showmigrations Lists all migrations and their status βœ…
python manage.py sqlmigrate myapp 0002 Shows the SQL Django will run
python manage.py migrate myapp 0002 Runs a specific migration
python manage.py makemigrations myapp Only make migrations for one app

πŸ”™ Rolling Back Migrations

Oops? Want to undo the last one?

python manage.py migrate myapp 0001

This rolls your app back to before migration 0002.


🧁 Best Practices

βœ… Always run makemigrations after editing models.py
βœ… Then run migrate right after
βœ… Don’t manually edit migration files unless you know what you’re doing
βœ… Keep migrations in version control (like Git!)


πŸ” Advanced Tip: Checking Migration Files

Go to your app folder:

πŸ“ myapp/migrations/0002_toy_rating.py

You’ll see something like this:

class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='toy',
            name='rating',
            field=models.FloatField(default=5.0),
        ),
    ]

It’s like a history book πŸ“– for your model!


🎯 Summary

Step Action
✍️ 1 Change your model in models.py
πŸͺ„ 2 Run python manage.py makemigrations
πŸ—οΈ 3 Run python manage.py migrate
πŸ” 4 Check with showmigrations or rollback if needed