#36372: refresh_from_db with explicit fields can't clear a relation unless it's
prefetched
-------------------------------------+-------------------------------------
Reporter: Roman Donchenko | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution:
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):
There's an important aspect of this problem that wasn't discussed here,
the fact that when a `related_name` is not specified for a `ForeignKey`
the reverse field name and the
[https://docs.djangoproject.com/en/5.2/ref/models/fields/#django.db.models.ForeignKey.related_query_name
query name] will differ.
In in case the reverse field name on `Article` is named `item` and its
query name and descriptor name is `item_set`. The changes implemented in
#29625 failed to account for the fact that the related query name, which
is used for the prefetched key namespace, might differ from the field name
for reverse relationships.
If an explicit `related_name` is defined (e.g. `items`) then both matches
and calling `refresh_from_db(fields=["items"])` doesn't crash with and
without the prior `prefetch_related("items")` call (albeit it performs an
unnecessary query of the form `SELECT id FROM container WHERE container =
?`).
Changing `ForeignObjectRel.name` to default to `.accessor_name` is
tempting here but that would likely break tons of things.
--
Ticket URL: <https://code.djangoproject.com/ticket/36372#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/01070196b6ec7543-2698cb2c-ec91-4da2-8b87-9ec31ee02bc4-000000%40eu-central-1.amazonses.com.