#34633: RelatedManager._remove_prefetched_objects() not called in
RelatedManager.create()
-------------------------------------+-------------------------------------
Reporter: Rob | Owner: nobody
Percival |
Type: Bug | Status: new
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
[https://docs.djangoproject.com/en/4.0/ref/models/querysets/#prefetch-
related The documentation] says:
> if you call the database-altering methods add(), remove(), clear() or
set(), on related managers, any prefetched cache for the relation will be
cleared.
This is accurate. However, there are other database-altering methods, such
as create(), for which the cache ought to be cleared but isn't. This
results in this confusing state of affairs:
{{{
x = MyModel.objects.prefetch_related("related_objects").get()
assert len(x.related_objects.all()) == 0
x.related_objects.create()
assert len(x.related_objects.all()) == 1 # Assertion fails because the
prefetch cache isn't cleared by create()
}}}
Using add() rather than create() would cause the above code to work as
expected. If there's a good reason for create() not clearing the cache,
could that be documented please? Otherwise, could it clear the cache? From
a search of Stack Overflow, this is definitely a source of confusion for
some.
--
Ticket URL: <https://code.djangoproject.com/ticket/34633>
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/010701888b7394d2-a84c5dc3-9a15-4e21-a77e-8451f19a4528-000000%40eu-central-1.amazonses.com.