#18437: Incorrect double JOIN when using multiple .filter() calls on the same
table
-------------------------------------+-------------------------------------
Reporter: Diederik van der | Owner: nobody
Boor |
Type: Bug | Status: closed
Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by borisalekseev):
May be useful for optimize sql with filtered relation.
Here is examples:
{{{
qs = MyModel.objects.annotate(actual_other=FilteredRelation("othermodel",
condition=Q("othermodel__delete_time__isnull=True"))
qs.filter(actual_other__field="foo")
}}}
In this case django produces sql with duplicated join for filtering and
for FilteredRelation.
But if we pass FilteredRelation alias to select_related, problem solved:
{{{
qs = MyModel.objects.annotate(actual_other=FilteredRelation("othermodel",
condition=Q("othermodel__delete_time__isnull=True")).select_related("actual_other")
qs.filter(actual_other__field="foo")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/18437#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/010701958f0d7192-a50c425f-11cd-49f2-9c4b-baadba7392d2-000000%40eu-central-1.amazonses.com.