From 4fd34b85ad64af11dac8e161b2fd15ac3db73fdc Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 13 Aug 2026 16:40:55 +0000 Subject: [PATCH] Fix count_where() raising OperationalError on nonexistent tables rows_where() and delete_where() already return gracefully when called on a table that does not exist yet; count_where() now does the same by returning 0 instead of propagating OperationalError from SQLite. --- sqlite_utils/db.py | 2 ++ tests/test_introspect.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 9c0b402..8aead18 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -2023,6 +2023,8 @@ class Queryable: :param where_args: Parameters to use with that fragment - an iterable for ``id > ?`` parameters, or a dictionary for ``id > :id`` """ + if not self.exists(): + return 0 sql = f"select count(*) from {quote_identifier(self.name)}" if where is not None: sql += " where " + where diff --git a/tests/test_introspect.py b/tests/test_introspect.py index 343424d..8428533 100644 --- a/tests/test_introspect.py +++ b/tests/test_introspect.py @@ -96,6 +96,12 @@ def test_count_where(existing_db): assert existing_db.table("foo").count_where("text != :t", {"t": "two"}) == 2 +def test_count_where_nonexistent_table(fresh_db): + assert fresh_db.table("does_not_exist").count_where() == 0 + assert fresh_db.table("does_not_exist").count_where("id > ?", [5]) == 0 + assert fresh_db.table("does_not_exist").count == 0 + + def test_columns(existing_db): table = existing_db.table("foo") assert [{"name": "text", "type": "TEXT"}] == [