From 5c6eb9a4ecd0a4f0e9048265b13aaf6ebd8ae5b1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 29 Aug 2026 14:33:05 +0000 Subject: [PATCH] fix: raise NoTable from rows_where() and delete_where() for non-existent tables rows_where() silently returned an empty iterator and delete_where() silently returned self when called on a table that does not exist. This masked bugs in callers that passed a wrong table name. Both methods now raise NoTable (matching the behaviour of count_where() and duplicate()), as planned for the v5 release. --- sqlite_utils/db.py | 4 ++-- tests/test_delete.py | 7 +++++++ tests/test_rows.py | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index c011d9bb0..d0604268f 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -2160,7 +2160,7 @@ def rows_where( :param offset: Integer for SQL offset """ if not self.exists(): - return + raise NoTable(f"Table {self.name} does not exist") sql = f"select {select} from {quote_identifier(self.name)}" if where is not None: sql += " where " + where @@ -4105,7 +4105,7 @@ def delete_where( :param analyze: Set to ``True`` to run ``ANALYZE`` after the rows have been deleted. """ if not self.exists(): - return self + raise NoTable(f"Table {self.name} does not exist") sql = f"delete from {quote_identifier(self.name)}" if where is not None: sql += " where " + where diff --git a/tests/test_delete.py b/tests/test_delete.py index a9341b875..58862651a 100644 --- a/tests/test_delete.py +++ b/tests/test_delete.py @@ -1,4 +1,6 @@ +import pytest import sqlite_utils +from sqlite_utils.db import NoTable def test_delete_rowid_table(fresh_db): @@ -62,3 +64,8 @@ def test_delete_where_analyze(fresh_db): assert list(fresh_db.table("sqlite_stat1").rows) == [ {"tbl": "table", "idx": "idx_table_i", "stat": "6 1"} ] + + +def test_delete_where_nonexistent_table(fresh_db): + with pytest.raises(NoTable): + fresh_db.table("does_not_exist").delete_where() diff --git a/tests/test_rows.py b/tests/test_rows.py index 476569ed5..2be14c208 100644 --- a/tests/test_rows.py +++ b/tests/test_rows.py @@ -147,3 +147,9 @@ def test_pks_and_rows_where_compound_pk_declaration_order(fresh_db): fresh_db.table("t").insert({"a": "A", "b": "B"}) pks_and_rows = list(fresh_db.table("t").pks_and_rows_where()) assert pks_and_rows == [(("A", "B"), {"b": "B", "a": "A"})] + + +def test_rows_where_nonexistent_table_raises(fresh_db): + from sqlite_utils.db import NoTable + with pytest.raises(NoTable): + list(fresh_db.table("does_not_exist").rows_where())