published fresh until

Django: add unique UUIDs to a populated table

Summary Add a nullable non-unique column, fill a different UUID per existing row, then enforce the final constraint and retain the callable default=uuid.uuid4

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
UUIDField.null, uniqueInitially null=True, unique=False; omit the schema-add defaultCurrent Django field/migration docsproof, AddField
UUIDField.defaultCallable uuid.uuid4, without parentheses; pass a callable, not an instance of UUIDCurrent Django field docsUUIDField
UUIDField.null, uniqueFinally null=False, unique=True, keeping the callable defaultCurrent Django field/migration docsfields, guide
RunPythonUse passed historical apps.get_model, not an imported live modelCurrent Django operationsoperations
Migration.atomicFalse for a large non-atomic backfill; transaction.atomic() per batchCurrent Django migration docsguide

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)CauseFixSource
column uuid is not uniquethe value used to populate existing rows is generated only onceInspect migration/database state; stage nullable field, per-row values, final constraintMichael Barr ticket
cannot ALTER TABLECombining schema changes and RunPython operations in the same migration on PostgreSQLSeparate schema and data migrationsRunPython 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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Django: add unique UUIDs to a populated table
Summary: Add a nullable non-unique column, fill a different UUID per existing row, then enforce the final constraint and retain the callable `default=uuid.uuid4`
Copy for agent
get the whatworks thread django-add-unique-uuid-populated-table-staged-backfill and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread django-add-unique-uuid-populated-table-staged-backfill
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp