Repository navigation
Partial upsert of an existing row no longer fails on an omitted NOT NULL column - #971
Merged
Merged
Conversation
simonw
force-pushed
the
upsert-not-null-omitted
branch
from
November 29, 2026 19:56
503fedb to
ed40457
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #971 +/- ##
==========================================
+ Coverage 94.76% 94.80% +0.03%
==========================================
Files 10 10
Lines 4529 4544 +15
==========================================
+ Hits 4292 4308 +16
+ Misses 237 236 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
simonw
force-pushed
the
upsert-not-null-omitted
branch
from
November 29, 2026 20:05
ed40457 to
1075118
Compare
The native INSERT ... ON CONFLICT DO UPDATE path failed with IntegrityError when an upsert omitted a NOT NULL column without a default, because SQLite checks NOT NULL on the would-be inserted row before the conflict clause applies. When the target table has such columns missing from the statement, add them to the INSERT column list filled by a scalar subquery against the existing row, and leave them out of DO UPDATE SET. Existing rows keep their value; a genuinely new row gets NULL from the subquery and still fails the constraint. SQL for the common case is unchanged. Refs #878 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
simonw
force-pushed
the
upsert-not-null-omitted
branch
from
November 29, 2026 20:13
1075118 to
1a7b73c
Compare
simonw
commented
Nov 29, 2026
Comment on lines
+4474
to
+4478
| required_cols = [ | ||
| col.name | ||
| for col in self.columns | ||
| if col.notnull and col.default_value is None | ||
| ] |
Owner
Author
There was a problem hiding this comment.
This looks like an introspection in a loop which would affect performance.
Refs #878 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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:
A partial
upsert()against an existing row raisedIntegrityErrorif it omitted aNOT NULLcolumn with no default, even though the row already had a value for it.Claude Fable 5.2 Medium
Current buggy behavior produces:
With this fix applied the upsert succeeds and
dogs.get(1)returns:{'id': 1, 'name': 'Cleo', 'color': 'black'}SQLite checks
NOT NULLon the row it would insert beforeON CONFLICTapplies, so theINSERT ... ON CONFLICT DO UPDATEintroduced in 4.0 failed where the older implementation did not.The change
In the native upsert path, any
NOT NULLcolumn without a default that the statement does not supply is added to theINSERTcolumn list, with its value read from the existing row by a scalar subquery on the primary key:DO UPDATE SET.NULL, so the upsert still raisesIntegrityErrorrather than inventing a value.last_pk/last_rowidhandling and atomicity are unchanged.Trade-offs
PRAGMA table_info) once perupsert()/upsert_all()call.insert_all()does the lookup once and passes it down, so it is not repeated for every chunk.use_old_upsert=Truepath is not changed. It already handled the existing-row case; its behaviour of inserting""fornot_nullcolumns on new rows is left alone.Testing
New tests in
tests/test_upsert.py: the repro on both upsert implementations, a new row missing the column still raising (forupsertand for anupsert_allmixing existing and new rows), aNOT NULLcolumn with a default, a compound pk, and a 250-rowupsert_allacross batches. Full suite: 1553 passed, 4 skipped. Not run locally against SQLite 3.23.1 - that is left to CI.Builds on #970 (now merged). The fill is applied per run of same-column records, so it also covers a mixed batch where only some records omit the
NOT NULLcolumn (tested).🤖 Generated with Claude Code
📚 Documentation preview 📚: https://sqlite-utils--971.org.readthedocs.build/en/971/