Opened 17 years ago

Closed 17 years ago

#5258 closed (worksforme)

SlugField not getting an index if attribute of unique=True also set

Reported by: odonian@… Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: SlugField, unique, index, PostgreSQL
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm using Postgres 8.2 and I'm noticing that for all Django
models where I have a SlugField, the database index seems to be
missing where I also set unique=True in the SlugField definition. By
default, SlugField has db_index=True, according to the docs, and I do
see an index for SlugFields where I didn't set unique=True. i.e.

foo = models.SlugField() ----> index is present in Postgres, as
expected

bar = models.SlugField(unique=True) ---> index is not present in
Postgres

I also have other attributes on the SlugField, like maxlength,
blank=True and null=True, but the only consistent difference between
the SlugFields where an index is not present automatically vs those
that are seems to be the attribute unique=True. Manually creating the
index works fine.

Change History (2)

comment:1 by Collin Grady <cgrady@…>, 17 years ago

Doesn't UNIQUE imply an index in postgres?

comment:2 by Matt McClanahan <cardinal@…>, 17 years ago

Resolution: worksforme
Status: newclosed

I can't reproduce this in trunk (rev 6020), with postgresql 8.1 or 8.2. Using the following model:

class SlugTest(models.Model):
    """
        slug    1       2       3       4       5       6
    -----------------------------------------------------
    unique      1       1       1       0       0       0
    blank       1       1       0       0       1       1
    null        1       0       1       0       1       0
    """
    slug1 = models.SlugField(max_length=8, unique=True, blank=True, null=True)
    slug2 = models.SlugField(max_length=8, unique=True, blank=True)
    slug3 = models.SlugField(max_length=8, unique=True, null=True) 
    slug4 = models.SlugField(max_length=8)
    slug5 = models.SlugField(max_length=8, blank=True, null=True)
    slug6 = models.SlugField(max_length=8, blank=True)

I get the following table:

                                Table "public.foo_slugtest"
 Column |         Type         |                         Modifiers                         
--------+----------------------+-----------------------------------------------------------
 id     | integer              | not null default nextval('foo_slugtest_id_seq'::regclass)
 slug1  | character varying(8) | 
 slug2  | character varying(8) | not null
 slug3  | character varying(8) | 
 slug4  | character varying(8) | not null
 slug5  | character varying(8) | 
 slug6  | character varying(8) | not null
Indexes:
    "foo_slugtest_pkey" PRIMARY KEY, btree (id)
    "foo_slugtest_slug1_key" UNIQUE, btree (slug1)
    "foo_slugtest_slug2_key" UNIQUE, btree (slug2)
    "foo_slugtest_slug3_key" UNIQUE, btree (slug3)
    "foo_slugtest_slug4" btree (slug4)
    "foo_slugtest_slug5" btree (slug5)
    "foo_slugtest_slug6" btree (slug6)

Please provide a reproducible example model if it's still a problem in the latest trunk.

Note: See TracTickets for help on using tickets.
Back to Top