Skip to content

fix(cli): avoid transform() when table missing (header-only CSV + detect-types)#730

Open
beejak wants to merge 1 commit intosimonw:mainfrom
beejak:fix/header-only-csv-detect-types
Open

fix(cli): avoid transform() when table missing (header-only CSV + detect-types)#730
beejak wants to merge 1 commit intosimonw:mainfrom
beejak:fix/header-only-csv-detect-types

Conversation

@beejak
Copy link
Copy Markdown

@beejak beejak commented Apr 21, 2026

Summary

Fixes #702.

When importing CSV with default --detect-types, a header-only file (no data rows) yields an empty insert. The table may not exist yet, but the CLI still called Table.transform() from the type tracker path, which asserts that the table exists.

Guard both code paths with db[table].exists() before calling transform():

  • insert_upsert_implementation (e.g. sqlite-utils insert ... --csv)
  • the multi-file memory command path that uses the same tracker + insert_all + transform sequence

Test

Added test_insert_csv_header_only_detect_types_no_crash: header-only CSV + default detection exits 0 and does not create a table (same net behavior as before for zero rows, without crashing).

Verification

pytest tests/test_cli.py::test_insert_csv_header_only_detect_types_no_crash -v

📚 Documentation preview 📚: https://sqlite-utils--730.org.readthedocs.build/en/730/

 simonw#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Insert CSV with --detect-types crashes for CSV files that contain only a header row

1 participant