Changes between Initial Version and Version 1 of Ticket #36207


Ignore:
Timestamp:
Feb 21, 2025, 4:32:46 PM (21 hours ago)
Author:
Jacob Walls
Comment:

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.)

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #36207 – Description

    initial v1  
    11Although 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.
    22
    3 Here is a rough test using the composite_pk models:
     3Here is a rough test using the composite_pk models (EDIT: prior version of test wasn't demonstrating anything):
    44{{{#!diff
    55diff --git a/tests/composite_pk/test_models.py b/tests/composite_pk/test_models.py
    6 index 27157a52ad..fdf58d1621 100644
     6index 27157a52ad..7cd97a31a9 100644
    77--- a/tests/composite_pk/test_models.py
    88+++ b/tests/composite_pk/test_models.py
    9 @@ -155,3 +155,9 @@ class CompositePKModelsTests(TestCase):
     9@@ -155,3 +155,8 @@ class CompositePKModelsTests(TestCase):
    1010         self.assertEqual(4, token.permission_set.count())
    1111         self.assertEqual(4, user.permission_set.count())
     
    1313+
    1414+    def test_refresh_foreign_object(self):
    15 +        self.comment_1.user = self.comment_2.user
    16 +        self.assertEqual(self.comment_1.user, self.comment_2.user)
     15+        self.comment_1.user = None
    1716+        self.comment_1.refresh_from_db()
    18 +        self.assertNotEqual(self.comment_1.user, self.comment_2.user)
     17+        self.assertEqual(self.comment_1.user, self.user_1)
    1918}}}
    2019----
    2120{{{#!py
     21E
    2222======================================================================
    23 FAIL: test_refresh_foreign_object (composite_pk.test_models.CompositePKModelsTests.test_refresh_foreign_object)
     23ERROR: test_refresh_foreign_object (composite_pk.test_models.CompositePKModelsTests.test_refresh_foreign_object)
    2424----------------------------------------------------------------------
    2525Traceback (most recent call last):
    26   File "/Users/.../django/tests/composite_pk/test_models.py", line 163, in test_refresh_foreign_object
    27     self.assertNotEqual(self.comment_1.user, self.comment_2.user)
    28     ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    29 AssertionError: <User: User object ((1, 1))> == <User: User object ((1, 1))>
     26  File "/Users/.../django/tests/composite_pk/test_models.py", line 161, in test_refresh_foreign_object
     27    self.comment_1.refresh_from_db()
     28    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
     29  File "/Users/.../py313/lib/python3.13/site-packages/django/db/models/base.py", line 737, in refresh_from_db
     30    db_instance = db_instance_qs.get()
     31  File "/Users/.../py313/lib/python3.13/site-packages/django/db/models/query.py", line 633, in get
     32    raise self.model.DoesNotExist(
     33        "%s matching query does not exist." % self.model._meta.object_name
     34    )
     35composite_pk.models.tenant.Comment.DoesNotExist: Comment matching query does not exist.
    3036
    3137----------------------------------------------------------------------
    32 Ran 1 test in 0.006s
     38Ran 1 test in 0.008s
    3339
    34 FAILED (failures=1)
     40FAILED (errors=1)
    3541}}}
Back to Top