Skip to content

Reject duplicate CSV/TSV column names instead of dropping values - #987

Open
Jun-shisheng wants to merge 1 commit into
simonw:mainfrom
Jun-shisheng:fix/duplicate-csv-headers
Open

Jun-shisheng wants to merge 1 commit into
simonw:mainfrom
Jun-shisheng:fix/duplicate-csv-headers

Conversation

@Jun-shisheng

@Jun-shisheng Jun-shisheng commented Dec 2, 2026 •

Copy link
Copy Markdown

Fixes #986.

CSV/TSV files with duplicate column names were accepted and lost data silently.

insert --csv / --tsv built rows with dict(zip(headers, row)), so the last value for a repeated header won. A file like:

name,name
dog,cat

created a single name column containing cat, and exited 0. sqlite-utils memory had the same problem because csv.DictReader also keeps only the last duplicate.

Both paths now reject duplicate headers:

  • insert / upsert exit 1 with Error: Duplicate column names: name before creating the table.
  • rows_from_file() raises RowError when iterated, and memory turns that into the same CLI error.

Tests

Added regression tests for insert --csv, insert --tsv, rows_from_file() CSV/TSV, and memory. Full suite passes:

python -m pytest -q
# 1635 passed, 17 skipped

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

suirindo commented Dec 3, 2026

Copy link
Copy Markdown

Should the SQLite import path also catch headers that differ only in ASCII case? At 7e34d89, the patched CSV helper rejects Name,Name, but yields two distinct keys for Name,name. Creating an in-memory SQLite table from those two keys then raises OperationalError: duplicate column name: name. A check at the SQLite import boundary could give the same clear error without changing the standalone CSV reader's behavior. AI-assisted review; I ran the reader helper plus SQLite, not the full CLI.

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name,name / dog,cat now exits 1 with Duplicate column names: name from insert --csv and from memory; main created one name column holding cat. The 5 new tests fail on main, and all 151 tests in the three touched files pass here. A single blank trailing header (a,b,) loads the same as before.

Comment thread sqlite_utils/cli.py
duplicates = _duplicate_headers(headers)
if duplicates:
raise click.ClickException(
"Duplicate column names: {}".format(", ".join(duplicates))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exports with two or more empty trailing headers, like a,b,, (common from spreadsheets), now hit this with Error: Duplicate column names: and nothing after the colon, the same from memory. Main loaded them, keeping only the last blank column's value, so rejecting them makes sense, but the message is hard to act on. Quoting the names (repr, or '"{}"') would show "".

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 and memory silently drop values when column names are duplicated

3 participants