#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 , 11 years ago
Summary: | Use the same convenience → Allow skipping outer parentesis when defining index_together |
---|
comment:2 by , 11 years ago
Summary: | Allow skipping outer parentesis when defining index_together → Allow skipping outer parenthesis when defining index_together |
---|
comment:3 by , 11 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:4 by , 11 years ago
Type: | Bug → New feature |
---|
comment:5 by , 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.
comment:6 by , 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 , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:8 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:9 by , 11 years ago
31 hours from suggestion to merged code?! This community is amazing! :) Thanks!
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.