From 2ab106a02b94b9cbad3146501b978098463ecc3c Mon Sep 17 00:00:00 2001 From: Enrique Soria Date: Sat, 8 Jun 2024 14:45:26 +0200 Subject: [PATCH] Refs #33579: Changed exception raised when no rows are affected to a more specialized one --- django/db/models/base.py | 6 +++--- django/db/utils.py | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index cd300e47bc..89c455f313 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -20,7 +20,6 @@ from django.core.exceptions import ( ) from django.db import ( DJANGO_VERSION_PICKLE_KEY, - DatabaseError, connection, connections, router, @@ -48,6 +47,7 @@ from django.db.models.signals import ( pre_save, ) from django.db.models.utils import AltersData, make_model_tuple +from django.db.utils import NoRowsAffected from django.utils.deprecation import RemovedInDjango60Warning from django.utils.encoding import force_str from django.utils.hashable import make_hashable @@ -1112,9 +1112,9 @@ class Model(AltersData, metaclass=ModelBase): base_qs, using, pk_val, values, update_fields, forced_update ) if force_update and not updated: - raise DatabaseError("Forced update did not affect any rows.") + raise NoRowsAffected("Forced update did not affect any rows.") if update_fields and not updated: - raise DatabaseError("Save with update_fields did not affect any rows.") + raise NoRowsAffected("Save with update_fields did not affect any rows.") if not updated: if meta.order_with_respect_to: # If this is a model with an order_with_respect_to diff --git a/django/db/utils.py b/django/db/utils.py index e45f1db249..e8eb62afec 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -26,6 +26,10 @@ class DatabaseError(Error): pass +class NoRowsAffected(DatabaseError): + pass + + class DataError(DatabaseError): pass