Skip to content
Closed
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
11 changes: 9 additions & 2 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ class TransactionError(Exception):
"Operation cannot be performed while a transaction is open"


class UpdateError(Exception):
"Update did not affect exactly one row"


class DescIndex(str):
pass

Expand Down Expand Up @@ -4169,8 +4173,11 @@ def update(
else:
raise

# TODO: Test this works (rolls back) - use better exception:
assert rowcount == 1
# If rowcount is not exactly 1, the row was not found or multiple rows matched:
if rowcount != 1:
raise UpdateError(
f"Expected to update 1 row, but updated {rowcount}"
)
self.last_pk = pk_values[0] if len(pks) == 1 else pk_values
return self

Expand Down
7 changes: 7 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,10 @@ def test_update_dictionaries_and_lists_as_json(fresh_db, data_structure):
row = fresh_db.execute("select id, data from test").fetchone()
assert row[0] == 1
assert data_structure == json.loads(row[1])


def test_update_error_class_exists():
"""Verify UpdateError is defined and can be raised"""
from sqlite_utils.db import UpdateError
with pytest.raises(UpdateError, match="Expected to update 1 row"):
raise UpdateError("Expected to update 1 row, but updated 0")
Loading