#35833: Annotation yielding an empty set causes entire QuerySet to become empty
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):
* resolution: => invalid
* status: new => closed
Comment:
Thanks very much, Simon. This is user error, then. I guess rather than a
Cartesian product I was expecting an implicit subquery so that anything
that happened during annotate would not "filter" the QuerySet. I didn't
intend to suggest there was a count/len mismatch; I was just trying to
show that I had some number of model instances persisted in general, and I
expected to see all of them no matter what happened in `annotate()`. But
your explanation helps me see why that's wrong.
Thanks for the draft query, too. While this ticket was being triaged, I
ended up using an explicit subquery, along these lines:
{{{
# Start from an FK-related model
ResourceInstance.objects.annotate(
annotation=ArraySubquery(
TileModel.objects.filter(resourceinstance_id=OuterRef("resourceinstanceid"))
.annotate(as_array=JsonbArrayElements(F("more_complex_path_lookup"))
.values("as_array")
)
)
}}}
Then, in some queries where my json path lookup needed to be resilient
against missing keys in I ended up wrapping the expression given to the
`JsonbArrayElements` in a `Case` as you suggested.
I would have come back and closed the ticket if I could have put all these
pieces together more quickly (sorry!), but I guess I was still bewitched
by the assumption that whatever I did inside `annotate()` wouldn't
possibly filter down my queryset.
--
Ticket URL: <https://code.djangoproject.com/ticket/35833#comment:4>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070192958a4b5c-a5c4caf4-590e-426a-a3d7-a6f2eef7a532-000000%40eu-central-1.amazonses.com.