What this thread does
Do first: split the generated AddField migration into a nullable non-unique field, a per-row RunPython backfill, and a final AlterField constraint.
Short answer
As of November 2026, Django 6.1.3, released November 3, still needs staged backfilling: a schema-add default can be evaluated once for existing rows (guide). Michael Barr's historical ticket demonstrates the duplicate-value failure; it differs from evaluating a callable too early in model code.
Diagnostic: column uuid is not unique
The field may have received the same UUID on existing rows; inspect the schema migration before retrying (Michael Barr ticket).
Choose / don't choose
- Small table, paused writers: use the simple per-row backfill; don't allow newly created UUIDs to be overwritten by a concurrent full-table pass.
- Large table: choose a non-atomic migration plus bounded atomic batches to limit transaction size. The official example uses 1000 rows per batch; size is a deployment decision (guide).
Not for: branched migration histories; see django-conflicting-migrations-multiple-leaf-nodes-makemigrations-merge.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
UUIDField.null, unique | Initially null=True, unique=False; omit the schema-add default | Current Django field/migration docs | proof, AddField |
UUIDField.default | Callable uuid.uuid4, without parentheses; pass a callable, not an instance of UUID | Current Django field docs | UUIDField |
UUIDField.null, unique | Finally null=False, unique=True, keeping the callable default | Current Django field/migration docs | fields, guide |
RunPython | Use passed historical apps.get_model, not an imported live model | Current Django operations | operations |
Migration.atomic | False for a large non-atomic backfill; transaction.atomic() per batch | Current Django migration docs | guide |
Order of operations
1. Set default=uuid.uuid4 and unique=True on the model field. Generate its schema migration, then two dependent empty migrations with makemigrations myapp --empty; substitute the real app label (guide). 2. Pause writers and back up the table. Make the first field nullable and non-unique, removing its schema-add default (AddField). 3. Backfill through apps.get_model("myapp", "MyModel"). Assign uuid.uuid4() separately inside the row loop; save() writes it. For restartable batches select only uuid__isnull=True (guide). 4. Check no null or duplicate remains, then apply AlterField with final constraints. Resume writers with the callable model default. On PostgreSQL, avoid combining schema changes and RunPython operations in the same migration (operations).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
column uuid is not unique | the value used to populate existing rows is generated only once | Inspect migration/database state; stage nullable field, per-row values, final constraint | Michael Barr ticket |
cannot ALTER TABLE | Combining schema changes and RunPython operations in the same migration on PostgreSQL | Separate schema and data migrations | RunPython warning |
Missing
- No zero-downtime writer-coordination design is implied. Backend lock duration and batch size need table-specific measurement; non-atomic migrations can leave completed batches after failure.
Checked
2026-11-12; current release, writing-migrations, field/operation references and historical ticket read. No Django or database commands executed.
Started from a post by @Django Software Foundation, packaged by the editor. Original post