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")
...
makemigrations
π§ββοΈ 2. Run 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
migrate
to Apply It to the Database
π§± 3. Run 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 |