Skip to content

insert --csv and memory silently drop values when column names are duplicated #986

Backlog

Description

@Jun-shisheng

What I did

Inserted a CSV file whose header row contains the same column name twice:

name,name
dog,cat
sqlite-utils insert data.db data example.csv --csv
sqlite-utils rows data.db data --nl

What I expected

An error. Two CSV columns cannot map to one SQLite column, and CREATE TABLE already rejects duplicate column names, so the import should not proceed silently.

What actually happened

Only the last value was kept, and the first was lost, with exit code 0:

{"name": "cat"}

sqlite-utils memory example.csv "select * from example" does the same, because csv.DictReader keeps only the last duplicate header.

The CLI builds rows with dict(zip(headers, row)) in insert_upsert_implementation, which collapses the duplicate before SQLite sees it. rows_from_file() uses csv.DictReader, which does the same.

Environment

  • sqlite-utils 4.2.2 (also on main)
  • Python 3.14.8, Windows 10
  • SQLite 3.50.4

Possible fix

Reject duplicate headers in insert / upsert and rows_from_file() with a clear error instead of silently dropping values.

Activity

  1. chcbz commented on Dec 5, 2026

    @chcbz

    I tested an additional regression case for #987: a malformed CSV passed to insert --csv --truncate should fail without deleting rows from the existing table.

    Starting each run with a fresh temporary database containing data(name) and one row, "keep me":

    printf 'name,name\ndog,cat\n' | sqlite-utils insert test.db data - --csv --truncate
    sqlite-utils rows test.db data --nl

    I also ran a control against that PR: insert --csv --no-headers still accepts the same bytes and stores name,name as data under untitled_1 and untitled_2. That distinguishes rejecting duplicate headers from rejecting repeated values. An assertion that the pre-existing row survives would complement the PR's current assertion that a new table is not created.

    I’m chcbz, the maintainer of Kit. I used OpenAI Codex to assist with the reproduction and drafting; the tests used only synthetic data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions