From a6b3812fc795a923fc3e7826e5aac32d1496b7ad Mon Sep 17 00:00:00 2001 From: beejak Date: Tue, 21 Apr 2026 22:33:20 +0530 Subject: [PATCH] fix(cli): skip transform when table missing after header-only CSV (fixes #702) insert_upsert_implementation and memory multi-file import call Table.transform() after TypeTracker runs. For a CSV with only a header row, insert_all yields no rows and may not create the table; transform() then asserted. Only run transform when the table exists. Covers both the insert command path and the memory command path. Made-with: Cursor --- sqlite_utils/cli.py | 4 ++-- tests/test_cli.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 9b9ee20e..1d731e69 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -1176,7 +1176,7 @@ def insert_upsert_implementation( ) else: raise - if tracker is not None: + if tracker is not None and db[table].exists(): db.table(table).transform(types=tracker.types) # Clean up open file-like objects @@ -2033,7 +2033,7 @@ def memory( rows = (_flatten(row) for row in rows) db.table(file_table).insert_all(rows, alter=True) - if tracker is not None: + if tracker is not None and db[file_table].exists(): db.table(file_table).transform(types=tracker.types) # Add convenient t / t1 / t2 views view_names = ["t{}".format(i + 1)] diff --git a/tests/test_cli.py b/tests/test_cli.py index 40c3595c..14b198ac 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2327,6 +2327,21 @@ def test_upsert_detect_types(tmpdir, option): ] +def test_insert_csv_header_only_detect_types_no_crash(tmpdir): + """Header-only CSV with default type detection must not crash (issue #702).""" + db_path = str(tmpdir / "test.db") + data = "name,age,weight\n" + result = CliRunner().invoke( + cli.cli, + ["insert", db_path, "creatures", "-", "--csv"], + catch_exceptions=False, + input=data, + ) + assert result.exit_code == 0 + db = Database(db_path) + assert not db["creatures"].exists() + + def test_csv_detect_types_creates_real_columns(tmpdir): """Test that CSV import creates REAL columns for floats (default behavior)""" db_path = str(tmpdir / "test.db")