Direct Answers
Does Django document a supported procedure for rolling back multiple migrations after a partial failure? No. Django does not provide a built-in command to reverse an entire batch of migrations as a single unit. Each migration is applied in its own transaction (on backends that support transactional DDL), so a failure rolls back only the failing migration. Completed migrations remain recorded in django_migrations and must be reversed individually if they are reversible.
Is migrate --fake intended for recovery after a partial failure? No. migrate --fake only inserts or removes rows in django_migrations without executing schema SQL. It is designed to reconcile migration state when the schema has been changed outside Django (e.g., manual SQL). Using it to "undo" a partially applied migration without verifying the actual schema matches the target state can hide divergence and cause later migrations to fail.
Are there version-specific limits on native rollback behavior in current Django releases? The core behavior has not changed in recent releases (4.2+, 5.x). Limits are governed by Migration.atomic, backend DDL transaction support, and operations explicitly marked non-atomic. No release introduces a batch-atomic rollback for a failed upgrade run.
Confirmed Facts
- Migrations are applied sequentially; each migration runs in its own transaction on backends that support transactional DDL (PostgreSQL, SQLite). On MySQL/MariaDB, DDL is generally non-transactional, so a failed migration can leave partial schema changes even if the migration is not recorded.
- When a migration fails, earlier migrations in the same run stay applied and recorded. Django does not automatically reverse them.
- Reversal is per migration via
migrate app_label migration_name and only works for migrations whose operations are reversible and supported by the backend.
migrate --fake does not run SQL; it only manipulates the migration history table.
Likely Explanation (Backend-Dependent)
- On transactional-DDL backends, a failed migration rolls back its own schema changes, but the database remains at the state after the last successful migration. The recorded history matches the schema.
- On non-transactional-DDL backends (MySQL/MariaDB), a migration can fail partway through its operations, leaving the schema in an intermediate state that does not match any recorded migration. Rolling back the migration record alone is insufficient.
- The supported recovery path after a partial failure is to reverse completed migrations individually (if reversible), otherwise restore from backup or point-in-time recovery and manually remove any partial schema changes before retrying.
Recovery Steps for This Case
- Identify the database backend. Run
python -m django --version and check DATABASES setting. The recovery approach differs for transactional vs. non-transactional DDL.
- Inspect migration history. Run
python -m django migrate --plan to see which migrations are marked applied. Query django_migrations directly if needed.
- Verify actual schema. Use database introspection (e.g.,
\d in psql, SHOW CREATE TABLE in MySQL) to compare against the expected state of the last applied migration.
- If on a transactional-DDL backend and schema matches history: Reverse completed migrations one by one with
python -m django migrate app_label migration_name (targeting the migration before the failed one). Only do this if the operations are reversible and data loss is acceptable.
- If on a non-transactional-DDL backend or schema diverges: Restore from a pre-upgrade backup or point-in-time recovery. Then remove any partial schema objects manually before re-running
migrate.
- Do not use
migrate --fake unless you have independently confirmed the schema matches the target migration state.
One Diagnostic Detail That Changes the Recommendation
What is the database backend (PostgreSQL, MySQL, MariaDB, SQLite, other)? This determines whether DDL transactions protect each migration and whether a failed migration can leave partial schema changes.