#33984: Related managers cache gets stale after saving a fetched model with new 
PK
-------------------------------------+-------------------------------------
     Reporter:  joeli                |                    Owner:  Bhuvnesh
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  4.1
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

 I found another regression in caching related manager. The following test
 works on Django 4.0 and crashes with
 4f8c7fd9d91b35e2c2922de4bb50c8c8066cbbc6:
 {{{#!diff
 diff --git a/tests/many_to_many/tests.py b/tests/many_to_many/tests.py
 index 53e870ddad..770808a92c 100644
 --- a/tests/many_to_many/tests.py
 +++ b/tests/many_to_many/tests.py
 @@ -92,6 +92,27 @@ class ManyToManyTests(TestCase):
          a5.authors.remove(user_2.username)
          self.assertSequenceEqual(a5.authors.all(), [])

 +    def test_related_manager_refresh(self):
 +        user_1 = User.objects.create(username="Jean")
 +        user_2 = User.objects.create(username="Joe")
 +        a5 = Article.objects.create(headline="Django lets you create web
 apps easily")
 +        a5.authors.add(user_1.username)
 +        self.assertSequenceEqual(user_1.article_set.all(), [a5])
 +        # Change the username on a different instance of the same user.
 +        user_1_from_db = User.objects.get(pk=user_1.pk)
 +        self.assertSequenceEqual(user_1_from_db.article_set.all(), [a5])
 +        user_1_from_db.username = "Paul"
 +        user_1_from_db.save()
 +        user_2.username = "Jean"
 +        user_2.save()
 +        # Add a different article.
 +        self.a4.authors.add(user_1_from_db.username)
 +        self.assertSequenceEqual(user_1_from_db.article_set.all(),
 [self.a4])
 +        # Refresh the first instance from DB.
 +        user_1.refresh_from_db()
 +        self.assertEqual(user_1.username, "Paul")
 +        self.assertSequenceEqual(user_1.article_set.all(), [self.a4])
 +
      def test_add_remove_invalid_type(self):
          msg = "Field 'id' expected a number but got 'invalid'."
          for method in ["add", "remove"]:

 }}}
 It shows two issues:
 - the second call of `user_1_from_db.article_set.all()` should use a new
 username (`Paul`) but it's still using the old one (`Jean`),
 - `user_1.refresh_from_db()` doesn't clear cached managers.

 I'm going to prepare revert of 4f8c7fd9d91b35e2c2922de4bb50c8c8066cbbc6
 and reopen #32980.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33984#comment:14>
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 on the web visit 
https://groups.google.com/d/msgid/django-updates/010701838d2fa004-96cd2d47-cb3b-45cd-9aaa-eed15e013347-000000%40eu-central-1.amazonses.com.

Reply via email to