[Django] #32932: Unique indexes

7 views
Skip to first unread message

Django

unread,
Jul 15, 2021, 1:25:29 PM7/15/21
to django-...@googlegroups.com
#32932: Unique indexes
-------------------------------------+-------------------------------------
Reporter: David | Owner: nobody
Sanders |
Type: New | Status: new
feature |
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Django already supports both unique constraints and indexes from the
models Meta <3

A worthwhile addition would be to support _unique indexes_, if the
database makes this distinction.

In Postgres a unique index is a distinct feature from unique constraints;
unique indexes are the mechanism that enforces the unique constraints
declared for the table: https://www.postgresql.org/docs/current/indexes-
unique.html.

One important difference between the 2 is the fact that indexes may
support expressions whereas constraints do not (at least in PG).

An example may be to enforce a unique attribute in a jsonb field:


{{{
CREATE UNIQUE INDEX unique_attr ON some_table (
(some_json_field->>'some_attr') )
}}}

This could perhaps be a new option on the `Index` class:

{{{
class SomeTable(models.Model):
some_json_field = models.JSONField()

class Meta:
indexes = (
models.Index(models.RawSQL("some_json_field->>'some_attr'",
params=[]), name="unique_attr", unique=True),
)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32932>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 15, 2021, 2:39:41 PM7/15/21
to django-...@googlegroups.com
#32932: Unique indexes
-------------------------------------+-------------------------------------
Reporter: David Sanders | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

Django 4.0 will support functional unique constraints which are
implemented internally by unique indexes, see #30916 and
3aa545281e0c0f9fac93753e3769df9e0334dbaa. I don't think there is a need
for a new API.

Duplicate of #30916.

--
Ticket URL: <https://code.djangoproject.com/ticket/32932#comment:1>

Reply all
Reply to author
Forward
0 new messages