Opened 14 months ago

Closed 14 months ago

Last modified 14 months ago

#22172 closed New feature (fixed)

Allow skipping outer parenthesis when defining index_together

Reported by: EmilStenstrom Owned by: anubhav9042
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 14 months ago by EmilStenstrom

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Use the same convenience to Allow skipping outer parentesis when defining index_together

comment:2 Changed 14 months ago by ceaess

  • Summary changed from Allow skipping outer parentesis when defining index_together to Allow skipping outer parenthesis when defining index_together

comment:3 Changed 14 months ago by wim@…

  • Triage Stage changed from Unreviewed to Accepted

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 14 months ago by anonymous

  • Type changed from Bug to New feature

comment:5 Changed 14 months ago by anubhav9042

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 14 months ago by anubhav9042 (previous) (diff)

comment:6 Changed 14 months ago by anubhav9042

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 14 months ago by anubhav9042

  • Owner changed from nobody to anubhav9042
  • Status changed from new to assigned

comment:8 Changed 14 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

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 14 months 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