Skip to content

insert: don't crash on header-only CSV with --detect-types#739

Closed
c-tonneslan wants to merge 1 commit into
simonw:mainfrom
c-tonneslan:fix/insert-empty-csv-detect-types
Closed

insert: don't crash on header-only CSV with --detect-types#739
c-tonneslan wants to merge 1 commit into
simonw:mainfrom
c-tonneslan:fix/insert-empty-csv-detect-types

Conversation

@c-tonneslan
Copy link
Copy Markdown

@c-tonneslan c-tonneslan commented May 17, 2026

When the CSV has a header but no data rows, the insert path never creates the table — the row loop has nothing to feed into the first insert. The post-insert `tracker.transform()` call then asserts the table exists and blows up:

AssertionError: Cannot transform a table that doesn't exist yet

Reported in #702 with the exact fix in the issue body. Guarded the transform on `db[table].exists()` so an empty CSV becomes a no-op — matching the behavior for a zero-byte CSV (no header either).

Added a parameterized test (both `-d` and `--detect-types`) so we don't regress.

Closes #702.


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

When the input has a header but no data rows, the insert path never
creates the table - the row loop has nothing to feed into the first
insert. The post-insert tracker.transform call then asserts the table
exists and blows up:

    AssertionError: Cannot transform a table that doesn't exist yet

Reported in simonw#702 with the exact fix in the body. Guard the transform
on db[table].exists() so an empty CSV is just a no-op, matching what
--detect-types already does for a zero-byte CSV (no header either).

Closes simonw#702.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
@c-tonneslan
Copy link
Copy Markdown
Author

Closing as a duplicate of #736 which I'd already filed earlier. #736 is more complete (covers both the insert path and the memory path).

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