Opened 5 years ago
Last modified 3 years ago
#31891 new Bug
Remove cached value of reverse side of 020 relation when updating attname.
Reported by: | Josh | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.1 |
Severity: | Normal | Keywords: | orm, related, onetoone |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Pull Requests: | How to create a pull request | ||
Description (last modified by ) ¶
Repro:
from django.db import models class AppUser(models.Model): pass class Profile(models.Model): user = models.OneToOneField( to=AppUser, on_delete=models.PROTECT, ) a = AppUser.objects.create() b = AppUser.objects.create() Profile.objects.create(user=a) b.profile # Errors, since it has not been created yet a.profile.user_id = b.id a.profile.save() # Works! Profile.objects.get(user=b) # <Profile: Profile object (1)> b.profile # Errors, since it uses the cached value
I can fix it by using a.profile.user = b
instead of a.profile.user_id = b.id
which does the cache invalidation as expected. Or using b.refresh_from_db()
after the save()
call. However I think it makes sense to fix this within the ORM because it's a subtle bug that can cause issues.
This is a contrived example but the real use-case was cloning a model instance. And instead of erroring it just kept returning the cached value
According to the ticket's flags, the next step(s) to move this issue forward are:
- To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is:
[https://github.com/django/django/pull/#### PR]
.
Change History (6)
comment:1 by , 5 years ago
Description: | modified (diff) |
---|
comment:2 by , 5 years ago
Description: | modified (diff) |
---|
comment:3 by , 5 years ago
Description: | modified (diff) |
---|
comment:4 by , 4 years ago
Type: | Uncategorized → Bug |
---|
comment:5 by , 4 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|
comment:6 by , 4 years ago
Summary: | Inconsistent related field cache invalidation → Remove cached value of reverse side of 020 relation when updating attname. |
---|---|
Triage Stage: | Unreviewed → Accepted |
Thanks for this ticket, I can see inconsistency but I'm not sure if it's feasible. Tentatively accepting for investigation.
The main difference is that
profile.user = b
uses one-to-one descriptors and has access to both side instances, on the other handprofile.user_id = b.id
uses a deferred attribute.This is probably fixed by #31863.