Why Django explicitly creates an index in a unique field

Update : Q simplification when experimenting with the psqlfollowing:

For the following Django model:

class Book(models.Model):
    name = models.TextField(unique=True)

pg_dump (PostgreSQL 9.3) shows the following table and limitations:

CREATE TABLE book (
    id integer NOT NULL,
    name text NOT NULL,
);

ALTER TABLE ONLY book ADD CONSTRAINT book_name_key UNIQUE (name);

CREATE INDEX book_name_like ON book USING btree (name text_pattern_ops);

But the PostgreSQL documentation says:

PostgreSQL automatically creates a unique index when a unique Constraint [...] is defined for the table.

[...] there is no need to manually create indexes for unique columns; it is just a duplicate of an automatically generated index.

: Django ? , , text_pattern_ops, Django . unique=True Django :

CREATE UNIQUE INDEX book_name_like ON book USING btree (name text_pattern_ops);

UNIQUE . , UNIQUE INDEX text_pattern_ops , UNIQUE.

+4
2

: https://code.djangoproject.com/ticket/24082

Triage: db_index=False ( unique=True)

+3

, Django:

, unique True, db_index, unique .

, Django unique=True db_index=True, db_index=True , Django text_pattern_ops (. 12234).

, PostgreSQL , :

, , , , <, lt; =, > > =, . xxx_pattern_ops.

unique=True, db_index=False.

+8

Source: https://habr.com/ru/post/1570029/


All Articles