#36157: Unusual behaviour when pre-fetching with only applied on the related
fields
-------------------------------+--------------------------------------
Reporter: Tim McCurrach | Owner: (none)
Type: Bug | Status: closed
Component: Uncategorized | Version: 5.1
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Simon Charette):
* resolution: => duplicate
* status: new => closed
Comment:
Hello Tim,
This is a duplicate of #33835 which was won't fixed.
> I can understand it might be a good idea to have the related-id's for
the blog on hand should you need them later. But I also think, that by
using .only() you are explicitly telling django - I don't need these. This
is a real problem for larger data-sets, where you end up with thousands of
extra round-trips to the database.
The thing is prefetching **must** have `blog_id` otherwise it has no way
to build the associative map between `Blog` instances and `Post` to
populate `blog.posts.all()`. In other words if I gave you a list of the
form `posts = [{"id": 1, "name": "Some blog Post"}, {"id": 2, "name":
"Some other blog Post"}` how would you partition it by `blog_id`?
Adapting `prefetch_related` to error out if provided an inadequate
`Prefetch(queryset)` could potentially be done but that would not solve
your actual problem.The third-party library you are using to automatically
generate these queries is flawed and should include `blog_id` in the
select mask (AKA the `only`) call if it relies on prefetching.
If you'd like to catch these early I'd suggest looking at the potential
solution to have warnings emitted on query leaks in
ticket:33835#comment:2.
--
Ticket URL: <https://code.djangoproject.com/ticket/36157#comment:3>
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 visit
https://groups.google.com/d/msgid/django-updates/01070194b4605b33-218c0a84-dd63-418a-8397-f6d1ef280b66-000000%40eu-central-1.amazonses.com.