#36207: refresh_from_db() doesn't refresh ForeignObject relations
-------------------------------------+-------------------------------------
     Reporter:  Jacob Walls          |                     Type:  Bug
       Status:  new                  |                Component:  Database
                                     |  layer (models, ORM)
      Version:  5.2                  |                 Severity:  Release
                                     |  blocker
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
 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)
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36207>
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/010701952a9dfe68-bf2a55fe-36ea-4904-9aaa-22d49a69970d-000000%40eu-central-1.amazonses.com.

Reply via email to