summaryrefslogtreecommitdiff
path: root/tests/update_only_fields
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2025-03-09 07:04:16 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2025-03-10 21:09:15 +0100
commitab148c02cedbac492f29930dcd5346e1af052635 (patch)
tree02125c972c2df1cee48a1740d3f0b945c4e0d864 /tests/update_only_fields
parentc1257350ca118868bdfc867926dffad38b215162 (diff)
Fixed #33579 -- Specialized exception raised on forced update failures.
Raising DatabaseError directly made it harder than it should to differentiate between IntegrityError when a forced update resulted in no affected rows. Introducing a specialized exception allows for callers to more easily silence, log, or turn them update failures into user facing exceptions (e.g. 404s). Thanks Mariusz for the review.
Diffstat (limited to 'tests/update_only_fields')
-rw-r--r--tests/update_only_fields/tests.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/tests/update_only_fields/tests.py b/tests/update_only_fields/tests.py
index 2c86995799..9595c767eb 100644
--- a/tests/update_only_fields/tests.py
+++ b/tests/update_only_fields/tests.py
@@ -1,3 +1,5 @@
+from django.core.exceptions import ObjectNotUpdated
+from django.db import DatabaseError, transaction
from django.db.models.signals import post_save, pre_save
from django.test import TestCase
@@ -292,3 +294,17 @@ class UpdateOnlyFieldsTests(TestCase):
employee_boss = Employee.objects.create(name="Boss", gender="F")
with self.assertRaisesMessage(ValueError, self.msg % "id"):
employee_boss.save(update_fields=["id"])
+
+ def test_update_fields_not_updated(self):
+ obj = Person.objects.create(name="Sara", gender="F")
+ Person.objects.filter(pk=obj.pk).delete()
+ msg = "Save with update_fields did not affect any rows."
+ # Make sure backward compatibility with DatabaseError is preserved.
+ exceptions = [DatabaseError, ObjectNotUpdated, Person.NotUpdated]
+ for exception in exceptions:
+ with (
+ self.subTest(exception),
+ self.assertRaisesMessage(DatabaseError, msg),
+ transaction.atomic(),
+ ):
+ obj.save(update_fields=["name"])