#36207: refresh_from_db() doesn't refresh ForeignObject relations
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.2
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Old description:

> Although it's an internal API `ForeignObject` is now
> [https://docs.djangoproject.com/en/5.2/topics/composite-primary-key
> /#composite-primary-keys-and-relations 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:
> {{{#!diff
> diff --git a/tests/composite_pk/test_models.py
> b/tests/composite_pk/test_models.py
> index 27157a52ad..fdf58d1621 100644
> --- a/tests/composite_pk/test_models.py
> +++ b/tests/composite_pk/test_models.py
> @@ -155,3 +155,9 @@ class CompositePKModelsTests(TestCase):
>          self.assertEqual(4, token.permission_set.count())
>          self.assertEqual(4, user.permission_set.count())
>          self.assertEqual(4, comment.permission_set.count())
> +
> +    def test_refresh_foreign_object(self):
> +        self.comment_1.user = self.comment_2.user
> +        self.assertEqual(self.comment_1.user, self.comment_2.user)
> +        self.comment_1.refresh_from_db()
> +        self.assertNotEqual(self.comment_1.user, self.comment_2.user)
> }}}
> ----
> {{{#!py
> ======================================================================
> FAIL: 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 163,
> in test_refresh_foreign_object
>     self.assertNotEqual(self.comment_1.user, self.comment_2.user)
>     ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> AssertionError: <User: User object ((1, 1))> == <User: User object ((1,
> 1))>
>
> ----------------------------------------------------------------------
> Ran 1 test in 0.006s
>
> FAILED (failures=1)
> }}}

New description:

 Although it's an internal API `ForeignObject` is now
 [https://docs.djangoproject.com/en/5.2/topics/composite-primary-key
 /#composite-primary-keys-and-relations 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):
 {{{#!diff
 diff --git a/tests/composite_pk/test_models.py
 b/tests/composite_pk/test_models.py
 index 27157a52ad..7cd97a31a9 100644
 --- a/tests/composite_pk/test_models.py
 +++ b/tests/composite_pk/test_models.py
 @@ -155,3 +155,8 @@ class CompositePKModelsTests(TestCase):
          self.assertEqual(4, token.permission_set.count())
          self.assertEqual(4, user.permission_set.count())
          self.assertEqual(4, comment.permission_set.count())
 +
 +    def test_refresh_foreign_object(self):
 +        self.comment_1.user = None
 +        self.comment_1.refresh_from_db()
 +        self.assertEqual(self.comment_1.user, self.user_1)
 }}}
 ----
 {{{#!py
 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)
 }}}

--
Comment (by Jacob Walls):

 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.)
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36207#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/010701952aa452b7-264ccada-558f-4931-8d87-67650bc7b4ac-000000%40eu-central-1.amazonses.com.

Reply via email to