#34099: update_or_create() not saving data assigned in a model's save() method
-------------------------------------+-------------------------------------
               Reporter:  Phil       |          Owner:  nobody
  Gyford                             |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  dev
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 In the current main branch (testing from commit 37c5b8c) it seems that a
 model's field assigned in its custom `save()` method isn't being saved to
 the database.

 I have this model, which should set `nickname` to the value of `name` on
 save:

 {{{#!python
 from django.db import models

 class Person(models.Model):

     name = models.CharField(blank=True, max_length=255)

     nickname = models.CharField(blank=True, max_length=255)

     def save(self, *args, **kwargs):
         self.nickname = self.name
         super().save(*args, **kwargs)
 }}}

 And here's a test which should pass, and does pass with Django 4.1:

 {{{#!python
 from django.test import TestCase
 from .models import Person

 class MyTestCase(TestCase):
     def test_name(self):
         person = Person.objects.create(name="Bob")

         self.assertEqual(person.nickname, "Bob")

         updated_person, created = Person.objects.update_or_create(
             id=person.id, defaults={"name": "Terry"}
         )
         updated_person.refresh_from_db()

         self.assertEqual(updated_person.name, "Terry")
         self.assertEqual(updated_person.nickname, "Terry")  # Fails here
 }}}

 But it fails:

 {{{
 ➜  ./manage.py test
 Found 1 test(s).
 Creating test database for alias 'default'...
 System check identified no issues (0 silenced).
 F
 ======================================================================
 FAIL: test_name (myapp.tests.MyTestCase)
 ----------------------------------------------------------------------
 Traceback (most recent call last):
   File "/Users/phil/Projects/personal/django-
 tester/myproject/myapp/tests.py", line 16, in test_name
     self.assertEqual(updated_person.nickname, "Terry")
 AssertionError: 'Bob' != 'Terry'
 - Bob
 + Terry
 }}}

 If the line `updated_person.refresh_from_db()` is removed in the test, the
 test passes.

 So it seems that `update_or_create()` is returning the correctly updated
 object, but for some reason it's not being saved to the database.

 I'm using SQLite for this test, although I first noticed the problem in a
 project using Postgresql.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34099>
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/01070183d7b8ea40-5338e1ea-3fc5-4687-8442-8ef9a18732ee-000000%40eu-central-1.amazonses.com.

Reply via email to