#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
-------------------------------------+-------------------------------------

Comment (by Lucas Miller):

 Sure,

 i've added setup datas to have a more explicit test :

 {{{
 cls.author3 = Author.objects.create(name='Rob')
 cls.book5 = Book.objects.create(
             title='Poem by Rob B',
             editor=cls.editor_b,
             author=cls.author3,
         )
 }}}

 then i'm doing

 {{{
 self.assertSequenceEqual(
             Author.objects.annotate(
                 book_alice=FilteredRelation('book',
 condition=Q(book__title__startswith='The book by')),
             ).exclude(book_alice__title__contains="Jane"),
             [self.author1]
         )
 }}}

 this query should first get filter book starting with 'The book by' so
 books numbers 2, 3 and 4 then exclude all books containing 'Jane' in the
 name. We should end up with only the book 4.

 The result here is book4 and book5 :

 {{{
 ======================================================================
 FAIL: test_with_join_exclude
 (filtered_relation.tests.FilteredRelationTests)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File
 "/home/lucas/dev/test/django_lucas/tests/filtered_relation/tests.py", line
 108, in test_with_join_exclude
     [self.author1]
 AssertionError: Sequences differ: <QuerySet [<Author: Alice>, <Author:
 Rob>]> != [<Author: Alice>]

 First sequence contains 1 additional elements.
 First extra element 1:
 <Author: Rob>

 - <QuerySet [<Author: Alice>, <Author: Rob>]>
 + [<Author: Alice>]
 }}}

 The generated SQL is :

 {{{
 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" U1
       WHERE
         U1."title" LIKE % Jane % ESCAPE '\'
     )
   )
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30349#comment:7>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.dc83f7256138277fca1f9034bf4a0ffd%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to