#34450: Lookup expressions across foreign keys introduce extra joins
-------------------------------------+-------------------------------------
     Reporter:  Roman Odaisky        |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  4.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 Haven't run the full test suite yet but it seems that intuitively passing
 `reuse` so the JOINs are reused does the trick for this particular issue
 at least

 {{{#!python
 diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
 index 3cb8bdbc8a..1ed7d7cb6e 100644
 --- a/django/db/models/sql/query.py
 +++ b/django/db/models/sql/query.py
 @@ -1373,7 +1373,7 @@ def build_filter(
              if not getattr(filter_expr, "conditional", False):
                  raise TypeError("Cannot filter against a non-conditional
 expression.")
              condition = filter_expr.resolve_expression(
 -                self, allow_joins=allow_joins, summarize=summarize
 +                self, allow_joins=allow_joins, reuse=can_reuse,
 summarize=summarize,
              )
              if not isinstance(condition, Lookup):
                  condition = self.build_lookup(["exact"], condition, True)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34450#comment:4>
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 on the web visit 
https://groups.google.com/d/msgid/django-updates/01070187386578d3-da6ad4e9-c16d-4691-97b0-6af6cec6b1e2-000000%40eu-central-1.amazonses.com.

Reply via email to