Opened 3 years ago
Last modified 3 days ago
#33579 assigned New feature
Raise a specialized exception when Model.save(update_fields) does not affect any rows — at Initial Version
Description ¶
When Model.save(update_fields)
is used to update an instance of a previously fetched model and the resulting UPDATE
doesn't affect any row a DatabaseError
is raised.
Since the resulting exception cannot be differentiated by its type
from any other exception raised during save
(e.g. a failed UPDATE
also results in a DatabaseError
) the only pattern to gracefully handle this rare edge case to catch all DatabaseError
and compare its .args[0]
(or str
representation) to a string that doesn't offer any stability guarantees.
In order to make it easier for advanced users that rely on the update_fields
feature to handle this case differently from others where a DatabaseError
is raised I propose that that we introduced a new DatabaseError
subclass that would be raised instead when an unexpected empty update occurs.
I believe both instances should be switched to this pattern (force_update
and update_fields
).
According to the ticket's flags, the next step(s) to move this issue forward are:
- To add tests to the patch, then uncheck the "Needs tests" flag on the ticket.
- To write documentation for the patch, then uncheck "Needs documentation" on the ticket.
- To improve the patch as described in the pull request review comments or on this ticket, then uncheck "Patch needs improvement".
If creating a new pull request, include a link to the pull request in the ticket comment when making that update. The usual format is:
[https://github.com/django/django/pull/#### PR]
.