#37027: refresh_from_db() with from_queryset + prefetch does not persist result 
in
instance
-------------------------------------+-------------------------------------
     Reporter:  Hugo Maingonnat      |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  refresh_from_db,     |             Triage Stage:
  prefetch                           |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

 There's is little value in using `prefetch_related` when dealing with a
 single object as this method is meant to be used to prevent N+1 query
 issues but in this case `N=1` so the number of queries will be the same.
 In other words, performing `foo.bar_set.all()` will issue the same query
 as what a prefetching would have done.

 When we added `refresh_from_db(from_queryset)` support (#28344) we knew it
 would open the door to a large surface area between `Model` and `QuerySet`
 APIs. Given the original intent was to support `for_update` and friends
 I'm not convinced we should add support for `prefetch_related` and
 certainly not for `Prefetch(to_attr)` as it doesn't follow the
 `refresh_from_db(fields)` convention.

 Support for `select_related` came for free as it's a normal attribute
 lookup on the retrieved model instance and there are real benefits to
 using it (reducing the number of queries). In the case of
 `prefetch_related` it requires extra retrieval and invalidation logic of
 many-to-many relationships and results in the same number of queries.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/37027#comment:1>
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/0107019d726ac205-67d33816-1063-4413-8e4d-6703bcab81f5-000000%40eu-central-1.amazonses.com.

Reply via email to