Introduction
Database migration in production helps update your app's database safely without losing data. It changes the database structure to match your new code.
Jump into concepts and practice - no test required
Database migration in production helps update your app's database safely without losing data. It changes the database structure to match your new code.
python manage.py makemigrations python manage.py migrate
makemigrations to create migration files based on model changes.migrate to apply those changes to the database.python manage.py makemigrations python manage.py migrate
python manage.py migrate app_name 0002python manage.py showmigrations
This example shows adding a new field stock to an existing model. Running migrations updates the database safely.
from django.db import models # models.py class Product(models.Model): name = models.CharField(max_length=100) price = models.DecimalField(max_digits=8, decimal_places=2) # After adding a new field: class Product(models.Model): name = models.CharField(max_length=100) price = models.DecimalField(max_digits=8, decimal_places=2) stock = models.IntegerField(default=0) # Commands to run in terminal: # python manage.py makemigrations # python manage.py migrate # This will add the 'stock' column to the Product table without losing existing data.
Always back up your production database before running migrations.
Test migrations on a staging environment first to avoid surprises.
Use --fake option carefully if you need to mark migrations as applied without running them.
Database migrations update your database structure safely.
Use makemigrations to create and migrate to apply changes.
Always test and back up before migrating in production.
python manage.py migrate in a Django production environment?migrate command applies changes to the database schema based on migration files already created.makemigrations creates migration files, but migrate applies them to the database.makemigrations scans model changes and creates migration files.migrate applies migrations, runserver starts server, flush clears data.python manage.py makemigrations
python manage.py migrate
migrate?migrate applies changes only if migration files exist. Without new migration files, no schema changes happen.makemigrations, the database stays unchanged after migrate.python manage.py migrate in production but got an error about conflicting migrations. What is the best way to fix this?migrate --merge helps combine conflicting migrations safely without deleting files or manual edits.null=True), migrate, then fill data, and finally alter to non-nullable (null=False).