Repository navigation
upsert_all() no longer nulls out columns a record omits - #970
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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
force-pushed
the
upsert-all-mixed-fields
branch
from
November 29, 2026 19:55
220b6e3 to
bb63c45
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes:
upsert_all()padded every record out to the batch-wide column list withNone, then assigned every non-pk column inON CONFLICT ... DO UPDATE SET. A record that omitted a column therefore overwrote the existing value withNULL.Claude Fable 5.2 Medium
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.use_old_upsert=True/ SQLite < 3.24 path had the same bug and is fixed the same way.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.pyrun against both upsert implementations: mixed keys in one batch, the same pk repeated in a batch, mixed keys across chunks, and mixed keys withconversionsand withhash_id. 10 of them fail onmainand pass here. Full suite: 1521 passed, 4 skipped.🤖 Generated with Claude Code
📚 Documentation preview 📚: https://sqlite-utils--970.org.readthedocs.build/en/970/