Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#22172 closed New feature (fixed)

Allow skipping outer parenthesis when defining index_together

Reported by: EmilStenstrom Owned by: ANUBHAV JOSHI
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

"For convenience, unique_together can be a single tuple when dealing with a single set of fields"

https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together

This convenience feature is missing from index_together. This code...

class Subscription(models.Model):
    ...
    class Meta:
        unique_together = ("dialog_content_type", "dialog_object_id")
        index_together = ("dialog_content_type", "dialog_object_id")

... crashes with this error message:

notifications.subscription: "index_together" refers to d, a field that doesn't exist.
notifications.subscription: "index_together" refers to i, a field that doesn't exist.
notifications.subscription: "index_together" refers to a, a field that doesn't exist.
notifications.subscription: "index_together" refers to l, a field that doesn't exist.
notifications.subscription: "index_together" refers to o, a field that doesn't exist.
notifications.subscription: "index_together" refers to g, a field that doesn't exist.

I think it would be nice if the two similar settings worked the same.

Change History (9)

comment:1 by EmilStenstrom, 11 years ago

Summary: Use the same convenienceAllow skipping outer parentesis when defining index_together

comment:2 by Cea Stapleton, 11 years ago

Summary: Allow skipping outer parentesis when defining index_togetherAllow skipping outer parenthesis when defining index_together

comment:3 by wim@…, 11 years ago

Triage Stage: UnreviewedAccepted

Additionally, for some reason unique_together is defined as a tuple of tuples, and index_together as a list of lists, which seems odd to me as well.

comment:4 by anonymous, 11 years ago

Type: BugNew feature

comment:5 by ANUBHAV JOSHI, 11 years ago

I too found this thing to be true, single tuple doesn't works.

Although a thing to note:
unique_together works as a single tuple and list both:

class Test(models.Model):
    t1 = models.CharField(max_length=255, blank=True)
    t2 = models.CharField(max_length=255, blank=True, null=True)
    t3 = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        unique_together = ['t1','t2','t3']

We could add this in docs.

Last edited 11 years ago by ANUBHAV JOSHI (previous) (diff)

comment:6 by ANUBHAV JOSHI, 11 years ago

In django.db.options.py:
we have normalize_unique_together() defined which converts a single tuple to tuple of tuples.

We can just call that function itself and normalize index_together as well.

I have opened a PR: https://github.com/django/django/pull/2381

comment:7 by ANUBHAV JOSHI, 11 years ago

Owner: changed from nobody to ANUBHAV JOSHI
Status: newassigned

comment:8 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In bb2ca9fe6cdd490526b44b30f207c8f743bfaa84:

Fixed #22172 -- Allowed index_together to be a single list (rather than list of lists)..

Thanks EmilStenstrom for the suggestion.

comment:9 by EmilStenstrom, 11 years ago

31 hours from suggestion to merged code?! This community is amazing! :) Thanks!

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