[Django] #37069: Document that UniqueConstraint may create unique indexes unassociated with actual database constraints

7 views
Skip to first unread message

Django

unread,
Apr 26, 2026, 8:02:03 AM (5 days ago) Apr 26
to django-...@googlegroups.com
#37069: Document that UniqueConstraint may create unique indexes unassociated with
actual database constraints
-------------------------------------+-------------------------------------
Reporter: Clifford | Owner: Clifford Gama
Gama |
Type: | Status: assigned
Cleanup/optimization |
Component: | Version: dev
Documentation |
Severity: Normal | Keywords: unique
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The
[https://docs.djangoproject.com/en/dev/ref/models/constraints/#uniqueconstraint
docs] for `UniqueConstraint`:

> Creates a unique constraint in the database.

This is not always true. Depending on the options used, `UniqueConstraint`
may instead create a unique index via `CREATE UNIQUE INDEX` rather than a
database constraint via `ALTER TABLE ... ADD CONSTRAINT ... UNIQUE`
([https://github.com/django/django/blob/e84dc8715e91d51364ba6bda2b2fb07e7a8b750e/django/db/backends/base/schema.py#L1913-L1916
source]).

For example, adding the following constraints to a model with a `name`
field:
{{{#!python
constraints = [
models.UniqueConstraint(models.F("name"), name="unique_name_exp"),
models.UniqueConstraint(fields=["name"], name="unique_name_field"),
]
}}}
produces the following SQL on PostgreSQL:
{{{#!sql
BEGIN;
-- Create constraint unique_name_exp on model mymodel
CREATE UNIQUE INDEX "unique_name_exp" ON "upref_mymodel" ("name");
-- Create constraint unique_name_field on model mymodel
ALTER TABLE "myapp_mymodel" ADD CONSTRAINT "unique_name_field" UNIQUE
("name");
COMMIT;
}}}

See also [https://github.com/django/new-features/issues/124 Allow unique
indexes (via UniqueConstraint) to be created/dropped CONCURRENTLY on
PostgreSQL] where this came up as something of an issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/37069>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 29, 2026, 3:57:51 PM (yesterday) Apr 29
to django-...@googlegroups.com
#37069: Document that UniqueConstraint may create unique indexes unassociated with
actual database constraints
-------------------------------------+-------------------------------------
Reporter: Clifford Gama | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: unique | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* stage: Unreviewed => Accepted

Comment:

I agree we can improve this a bit. Maybe "create a uniqueness rule"?
--
Ticket URL: <https://code.djangoproject.com/ticket/37069#comment:1>
Reply all
Reply to author
Forward
0 new messages