#35376: Prefetched data not used when combining prefetch_related() and only()
-------------------------------------+-------------------------------------
     Reporter:  Michael Schwarz      |                    Owner:  nobody
         Type:  New feature          |                   Status:  closed
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     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
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

 Ah I think this is actually a duplicate of #35044 which is only resolved
 in `main`.

 What is happening here is that in the third case `restaurant` is
 prefetched and stored on the instance but then accessing `city_id` to
 prefetch the right `city` requires calling `refresh_from_db` (as it's
 excluded from the select mask) then #35044 (which clears all prefetched
 data on each `refresh_from_db` call) is triggered and the prefetched
 `restaurant` instance is discarded causing the access to the property in
 the test to refetch from the database.

 The only solution in 4.2 is to include `city` in your select mask (like
 you'd want to do anyway to avoid a N+1) to avoid the bugged
 `refresh_from_db` call.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35376#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 on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018ee2ff602c-11caf29d-6550-47ba-b1a6-8a3904c9224f-000000%40eu-central-1.amazonses.com.

Reply via email to