1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

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.
This commit is contained in:
Simon Charette
2025-03-09 07:04:16 -04:00
committed by Mariusz Felisiak
parent c1257350ca
commit ab148c02ce
8 changed files with 93 additions and 4 deletions

View File

@@ -1,3 +1,4 @@
from django.core.exceptions import ObjectNotUpdated
from django.db import DatabaseError, IntegrityError, models, transaction
from django.test import TestCase
@@ -50,8 +51,14 @@ class ForceTests(TestCase):
# the data isn't in the database already.
obj = WithCustomPK(name=1, value=1)
msg = "Forced update did not affect any rows."
with self.assertRaisesMessage(DatabaseError, msg):
with transaction.atomic():
# Make sure backward compatibility with DatabaseError is preserved.
exceptions = [DatabaseError, ObjectNotUpdated, WithCustomPK.NotUpdated]
for exception in exceptions:
with (
self.subTest(exception),
self.assertRaisesMessage(DatabaseError, msg),
transaction.atomic(),
):
obj.save(force_update=True)