Opened 12 years ago

Closed 12 years ago

#19982 closed Bug (duplicate)

Chained Queryset filters generate wrong join with reverse relations

Reported by: fhahn Owned by:
Component: Database layer (models, ORM) Version: dev
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

Change History (1)

comment:1 by Simon Charette, 12 years ago

Resolution: duplicate
Status: newclosed

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

Closing as duplicate of #18437.

Note: See TracTickets for help on using tickets.
Back to Top