Skip to content

upsert_all() no longer nulls out columns a record omits - #970

Merged
simonw merged 3 commits into
mainfrom
upsert-all-mixed-fields
Nov 29, 2026
Merged

simonw merged 3 commits into
mainfrom
upsert-all-mixed-fields

Conversation

@simonw

@simonw simonw commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Fixes:

upsert_all() padded every record out to the batch-wide column list with None, then assigned every non-pk column in ON CONFLICT ... DO UPDATE SET. A record that omitted a column therefore overwrote the existing value with NULL.

Claude Fable 5.2 Medium
table.insert_all([{"id": 1, "name": "Cleo", "age": 5}, {"id": 2, "name": "Nixie", "age": 6}], pk="id")
table.upsert_all([{"id": 1, "age": 7}, {"id": 2, "name": "Nixie II"}])

Current buggy behavior produces:

[{'id': 1, 'name': None, 'age': 7}, {'id': 2, 'name': 'Nixie II', 'age': None}]

With this fix applied you get:

[{'id': 1, 'name': 'Cleo', 'age': 7}, {'id': 2, 'name': 'Nixie II', 'age': 6}]

The same thing happened across chunks, because insert_all() accumulates columns from earlier chunks.

The change

build_insert_queries_and_params() now splits an upsert chunk into consecutive runs of records that supply the same columns and emits one statement per run, assigning only that run's columns.

  • Runs are consecutive rather than grouped globally, so records are still applied in order when a primary key repeats within a batch.
  • A batch where every record has the same keys still produces a single statement, identical to before (a test pins the exact SQL).
  • The legacy use_old_upsert=True / SQLite < 3.24 path had the same bug and is fixed the same way.
  • List mode and non-upsert inserts are unchanged.

Trade-off

A batch whose records alternate between key sets now sends one statement per run - in the worst case one per record.

Testing

New tests in tests/test_upsert.py run against both upsert implementations: mixed keys in one batch, the same pk repeated in a batch, mixed keys across chunks, and mixed keys with conversions and with hash_id. 10 of them fail on main and pass here. Full suite: 1521 passed, 4 skipped.

🤖 Generated with Claude Code


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

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.76%. Comparing base (cee9718) to head (24dd589).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #970      +/-   ##
==========================================
+ Coverage   94.74%   94.76%   +0.01%     
==========================================
  Files          10       10              
  Lines        4514     4529      +15     
==========================================
+ Hits         4277     4292      +15     
  Misses        237      237              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

When records in a single upsert_all() batch supplied different keys,
every record was padded to the batch-wide column list with None and
the upsert assigned all of those columns, overwriting existing values
with NULL. The same happened across chunks, since all_columns
accumulates columns seen in earlier chunks.

Upserts in dict mode now split each chunk into consecutive runs of
records that supply the same columns and emit one statement per run,
so each record only assigns the columns it provided. Runs are
consecutive rather than globally grouped so records are still applied
in order. Homogeneous batches still produce a single, unchanged
statement. The use_old_upsert / SQLite < 3.24 path likewise only
UPDATEs the columns each record supplied.

Refs #873

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@simonw
simonw force-pushed the upsert-all-mixed-fields branch from 220b6e3 to bb63c45 Compare November 29, 2026 19:55
@simonw simonw added the bug Something isn't working label Nov 29, 2026
simonw and others added 2 commits November 29, 2026 12:04
Refs #873

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
db[name] is typed as Table | View, so pyright rejected .get() and
.upsert_all() in the tests added for #946.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@simonw
simonw merged commit 9d62418 into main Nov 29, 2026
82 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant