Code

#19982 closed Bug (duplicate)

Chained Queryset filters generate wrong join with reverse relations

Reported by: fhahn Owned by:
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: flo@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Chaining .filter() on a Queryset can have a different result than one .filter() with all the attributes of the chained filters.

eg:

        n1 = Note.objects.create(note='n1', misc='foo', id=1)
        n2 = Note.objects.create(note='n2', misc='bar', id=2)

        a1 = Author.objects.create(name='a1')
        a3 = Author.objects.create(name='a3')
        Item.objects.create(name='i1', note=n1, creator=a1)
        Item.objects.create(name='i3', note=n1, creator=a3)

    Note.objects.filter(item__creator=a1).filter(item__name='i3') != Note.objects.filter(item__creator=a1, item__name='i3')

I think in this case, when chaining .filter() , another JOIN for item is added instead of reusing the previous JOIN.

I've created a test for this issue on Github: https://github.com/fhahn/django/commit/85554864c7f94546e9b663c2743e07a154c4f301

Attachments (0)

Change History (1)

comment:1 Changed 14 months ago by charettes

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

This is the expected behavior. See spanning multi valued relationships for more details.

Closing as duplicate of #18437.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.