Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#32684 closed Bug (duplicate)

Annotate query with Subquery with None value cannot be reliably filtered (or excluded)

Reported by: Gerben Morsink Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords: annotate, Subquery
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Gerben Morsink)

Let's say I have the following query

        last_location = Subquery(PersonLocation.objects.filter(
            person=OuterRef('id'), departure_date=None).order_by('-arrival_date').values('location')[:1])

        persons1 = Person.objects_with_inactive.annotate(current_location=last_location)

Where PersonLocation is the table connecting the Person to a Location (to keep track of the location history of a person).

And further in the code I do:

        persons2 = persons1.exclude(current_location=location.id)

This will always return an empty queryset. Even if in persons1 there are person objects that have current_location = None annotated.
This is very counter-intuitive, right?

I'm also not aware of a way to fix this. I tried persons1.filter(~Q(current_location=location.id)), but the result is the same.

NOTE: the location.id is a UUID, not sure if that makes a difference though.

Change History (3)

comment:1 by Gerben Morsink, 3 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 3 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #32398.

in reply to:  2 comment:3 by Gerben Morsink, 3 years ago

Replying to Mariusz Felisiak:

Duplicate of #32398.

I'm sorry I didn't find that issue. Thank you.

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