Opened 13 hours ago
Last modified 13 hours ago
#36207 new Bug
refresh_from_db() doesn't refresh ForeignObject relations
Reported by: | Jacob Walls | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 5.2 |
Severity: | Release blocker | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Although it's an internal API ForeignObject
is now quasi-public. When trying it out in a non-composite-pk situation I found that refresh_from_db()
didn't clear out a related ForeignObject.
Here is a rough test using the composite_pk models (EDIT: prior version of test wasn't demonstrating anything):
-
tests/composite_pk/test_models.py
diff --git a/tests/composite_pk/test_models.py b/tests/composite_pk/test_models.py index 27157a52ad..7cd97a31a9 100644
a b class CompositePKModelsTests(TestCase): 155 155 self.assertEqual(4, token.permission_set.count()) 156 156 self.assertEqual(4, user.permission_set.count()) 157 157 self.assertEqual(4, comment.permission_set.count()) 158 159 def test_refresh_foreign_object(self): 160 self.comment_1.user = None 161 self.comment_1.refresh_from_db() 162 self.assertEqual(self.comment_1.user, self.user_1)
E ====================================================================== ERROR: test_refresh_foreign_object (composite_pk.test_models.CompositePKModelsTests.test_refresh_foreign_object) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/.../django/tests/composite_pk/test_models.py", line 161, in test_refresh_foreign_object self.comment_1.refresh_from_db() ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ File "/Users/.../py313/lib/python3.13/site-packages/django/db/models/base.py", line 737, in refresh_from_db db_instance = db_instance_qs.get() File "/Users/.../py313/lib/python3.13/site-packages/django/db/models/query.py", line 633, in get raise self.model.DoesNotExist( "%s matching query does not exist." % self.model._meta.object_name ) composite_pk.models.tenant.Comment.DoesNotExist: Comment matching query does not exist. ---------------------------------------------------------------------- Ran 1 test in 0.008s FAILED (errors=1)
Note:
See TracTickets
for help on using tickets.
The variant where I found this didn't cause
refresh_from_db()
to throw, it just silently kept the old value. (This was with a nullable ForeignObject.)