Opened 3 years ago

Closed 3 years ago

Last modified 3 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: master
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 Changed 3 years ago by EmilStenstrom

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Summary: Use the same convenienceAllow skipping outer parentesis when defining index_together

comment:2 Changed 3 years ago by Cea Stapleton

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

comment:3 Changed 3 years ago by wim@…

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 Changed 3 years ago by anonymous

Type: BugNew feature

comment:5 Changed 3 years ago by ANUBHAV JOSHI

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 3 years ago by ANUBHAV JOSHI (previous) (diff)

comment:6 Changed 3 years ago by ANUBHAV JOSHI

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 Changed 3 years ago by ANUBHAV JOSHI

Owner: changed from nobody to ANUBHAV JOSHI
Status: newassigned

comment:8 Changed 3 years ago by Tim Graham <timograham@…>

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 Changed 3 years ago by EmilStenstrom

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

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