From 0e60f3c80cd6df5c177d8405afc54d014addebd0 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 13 Apr 2022 15:39:48 -0700 Subject: [PATCH] Better error message if table has no columns, closes #424 --- sqlite_utils/db.py | 1 + tests/test_create.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index dcbc2b6..82156b9 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -768,6 +768,7 @@ class Database: # Soundness check not_null, and defaults if provided not_null = not_null or set() defaults = defaults or {} + assert columns, "Tables must have at least one column" assert all( n in columns for n in not_null ), "not_null set {} includes items not in columns {}".format( diff --git a/tests/test_create.py b/tests/test_create.py index c3871e1..60181de 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -1149,3 +1149,9 @@ def test_create_if_not_exists(fresh_db): fresh_db["t"].create({"id": int}) # This should not fresh_db["t"].create({"id": int}, if_not_exists=True) + + +def test_create_if_no_columns(fresh_db): + with pytest.raises(AssertionError) as error: + fresh_db["t"].create({}) + assert error.value.args[0] == "Tables must have at least one column"