Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,18 @@ def testRollBack(self):
model.NotExistError, Author.FromPrimary, self.connection, new_author.key
)

def testFailedUpdate(self):
"""Author_two's values should default back to the original record stored
values after the update has failed."""
Author.Create(self.connection, {"name": "W. Shakespeare"})
author_two = Author.Create(self.connection, {"name": "W. Shakespeare"})
author_two.update({"ID": 1})
try:
author_two.Save()
except self.connection.IntegrityError:
pass
self.assertDictEqual(dict(author_two), {"ID": 2, "name": "W. Shakespeare"})


class NonStandardTableAndRelations(unittest.TestCase):
"""Verified autoloading works for records with an alternate table name."""
Expand Down
13 changes: 9 additions & 4 deletions uweb3/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1213,10 +1213,15 @@ def _SaveSelf(self, cursor):
"""
self._PreSave(cursor)
difference = self._Changes()
if difference:
self._RecordUpdate(cursor)
self._record.update(difference)
self._PostSave(cursor)
try:
if difference:
self._RecordUpdate(cursor)
self._record.update(difference)
self._PostSave(cursor)
except Exception as exc:
# After a failed save rollback the record to the stored database values
self.update(self._record)
raise exc

# ############################################################################
# Public methods for creation, deletion and storing Record objects.
Expand Down