Topic · Data & state

Migrations & schema changes for Django in production

Applying Django migrations safely during deploys, and repairing migration history when it goes wrong.

expand, migrate data, contractRelease 1add new columnBackfillin batchesRelease 2read the new oneRelease 3drop the old oneevery release can roll back to the one before itOld and new code share one schema during every deploy

Migrations change the schema under running code. In production that means keeping old and new code compatible, avoiding long locks on large tables, and having a plan when a migration fails halfway.

The guides cover the release strategy, safe schema changes, and fixing conflicts and inconsistent history.

2Set it up
9Run it well
1Check it
18Fix it

Set it up

Guides that take this from nothing to running in production.

  • Run Django Migrations as a Kubernetes Job

    Run Django migrations exactly once per release on Kubernetes: a Job per image tag, waiting for completion in CI, lock timeouts, why init containers are risky, and handling failures.

  • Migrate a Django Project From SQLite to PostgreSQL

    Move a live Django project from SQLite to PostgreSQL without losing data: create the schema with migrate, transfer data with dumpdata and loaddata or pgloader, fix content types and sequences, and cut over with minimal downtime.

Run it well

Tuning, reliability, and operations once it works.

  • Django Migration Strategy for Safe Deployments

    A good app deploy can still fail if the database migration step is unsafe. That is the real problem behind many Django migration deployment issues in production.

  • Run Schema Changes on Large Tables in Django Safely

    Change large PostgreSQL tables from Django migrations without outages: lock_timeout, concurrent indexes, NOT VALID constraints, and batched backfills.

  • Add Database Indexes in Django Without Locking Tables

    Add PostgreSQL indexes to large tables in a live Django app without blocking writes: AddIndexConcurrently in non-atomic migrations, handling failed concurrent builds, finding missing indexes, and removing unused ones safely.

  • Roll Back a Django Migration in Production

    Undo a Django migration safely in production: migrate an app back to a previous migration, understand which operations are reversible, handle data loss from reverting added columns and data migrations, roll back code and schema in the right order, and prefer forward fixes.

  • Squash Django Migrations Safely in Production

    Squash long Django migration histories without breaking deployed environments: run squashmigrations, keep replaced migrations until every database has applied them, handle RunPython and elidable operations, update dependencies, and remove old files in a later release.

  • Run a Django Data Migration on a Large Table

    Backfill or transform millions of rows in Django without long locks or failed deploys: move heavy data changes out of schema migrations, batch updates by primary key, run them as resumable management commands or Celery jobs, and keep code compatible during the backfill.

  • Rename a Django Model Field Without Downtime

    Rename a Django model field or column without breaking running code during rolling deploys: use db_column to rename in Python only, or expand and contract with a new column, dual writes, backfill, and removal across several releases.

  • Remove a Django Field Without Downtime

    Drop a Django model field and its database column safely during rolling deploys: stop using the field in code first, remove it from Django’s state with SeparateDatabaseAndState, drop the column in a later release, and handle NOT NULL columns and dependent indexes.

  • Migrate a Django Integer Primary Key to BigInt Without Downtime

    Convert a Django AutoField primary key to BigAutoField before it overflows: estimate time to exhaustion, avoid the table-rewriting ALTER on big tables, add a new bigint column with triggers, backfill, swap keys and foreign keys, and handle sequences.

Check it

Review lists to run before launch or after a change.

  • Django Migration Review Checklist

    A code-review checklist for Django migrations before they reach production: backward compatibility with running code, locking and table rewrites, large-table operations, data migrations, reversibility, dependencies, and deployment order.

Fix it

Errors and symptoms, each traced to a cause and a fix.

Learning paths through this topic

Related topics