Opened 9 years ago

Closed 15 months ago

#4070 closed New feature (wontfix)

unique_for_date and friends should create database indexes where possible

Reported by: sam@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Currently the field options unique_for_date, etc. do not create database indexes. I think this should be changed where the database backend supports this.

For example, with PostgreSQL:

db=> CREATE TABLE FOO (date timestamp with time zone, slug text);
db=> CREATE UNIQUE INDEX foo_unqiue_date ON foo (date_trunc ('day', date AT TIME ZONE 'UTC'), slug);
db=> INSERT INTO FOO VALUES (now(), 'slug1');
db=> INSERT INTO FOO VALUES (now(), 'slug2');
db=> INSERT INTO FOO VALUES (now(), 'slug1');
ERROR:  duplicate key violates unique constraint "foo_unique_date"

The date_trunc function can be used to truncate the timestamp data to any desired precision, so it can be used for the other unqiue_for attributes.

Change History (7)

comment:1 Changed 9 years ago by Simon G. <dev@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 8 years ago by anonymous

  • Owner changed from adrian to anonymous
  • Status changed from new to assigned

comment:3 Changed 8 years ago by anonymous

  • Owner changed from anonymous to adrian
  • Status changed from assigned to new

comment:4 Changed 5 years ago by lrekucki

  • Severity set to Normal
  • Type set to New feature

comment:5 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:6 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:7 Changed 15 months ago by aaugustin

  • Resolution set to wontfix
  • Status changed from new to closed

Since time zone support was added, the check is performed in the current time zone. In general, it's impossible to make a database index change dynamically when the time zone changes in the application. If you have a more restricted use case, you can add the index with a migration.

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