#30349: Using exclude on annotated FilteredRelation doesn't work
-------------------------------------+-------------------------------------
     Reporter:  Lucas Miller         |                    Owner:  robinh00d
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * stage:  Ready for checkin => Accepted


Comment:

 > So the result returned is not the result we are expecting, and the query
 should look like :

 Could you submit an additional test that demonstrate the wrong results are
 returned.

 The query you are suggesting seems problematic because the
 `book_alice."id" IS NULL` predicate contradicts the use of an `INNER JOIN`
 which should already filters out null results. In other words this query
 cannot match any row because the `INNER JOIN` will only return results in
 both sets and thus `book_alice."id" IS NOT NULL` will always be true.

 This kind of match can only be generated using a `LEFT OUTER JOIN` which
 some form of row grouping or a subquery pushdown which is what the ORM
 prefers to do.

 I think the appropriate query should be

 {{{#!sql
 SELECT
   "filtered_relation_author"."id",
   "filtered_relation_author"."name",
   "filtered_relation_author"."content_type_id",
   "filtered_relation_author"."object_id"
 FROM
   "filtered_relation_author"
 WHERE NOT (
     "filtered_relation_author"."id" IN (
         SELECT
           U1."author_id"
         FROM
           "filtered_relation_book"
         WHERE
           "filtered_relation_book"."title" LIKE 'poem by alice'
     )
 )
 }}}

 I guess `split_exclude` need to be adjusted to special case `filter_expr`
 that refer to a filtered expression instead of simply copy'ing
 `filtered_expressions` around.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30349#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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.9f8ccc1c954e1d51f3428a9729cd3537%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to