Conversation
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 #878.
Problem
When performing a partial
.upsert()on an existing row where an omitted column isNOT NULLwithout a default value, SQLite raisessqlite3.IntegrityError: NOT NULL constraint failed: [table].[column].This contradicts the documented guarantee:
Root Cause
In modern SQLite (
>=3.24) with nativeINSERT ... ON CONFLICT(pk) DO UPDATE, SQLite evaluatesNOT NULLconstraints on omitted columns during theINSERTclause evaluation before resolving the conflict on primary keys. When an omitted column isNOT NULLwithout aDEFAULT, SQLite aborts with an integrity error before reaching theDO UPDATEclause.Fix
In
Table.build_insert_queries_and_params():notnull, have no default value, are not part of the primary key, and are omitted fromall_columns(including checking caller-suppliednot_null).INSERTcolumn projection and supply a correlated scalar subquery for their values:NOT NULLcheck and triggeringON CONFLICTto executeDO UPDATE SET ...for caller-supplied columns only.NULL, correctly preservingIntegrityErrorwhen attempting to insert new rows missing required columns.CHECKconstraints,STRICTtables, and custom SQL conversions.Tests
Added comprehensive test coverage to
tests/test_upsert.py:test_upsert_omitted_not_null_column_on_existing_row: validates updating an existing row preserves omittedNOT NULLcolumns on bothuse_old_upsert=(False, True).test_upsert_all_omitted_not_null_compound_pk: validates batch upsert (batch_size=(1, 2)) with compound primary keys and multiple omittedNOT NULLcolumns.test_upsert_omitted_not_null_with_conversions_and_explicit_not_null: validates interactions with column conversions and explicitnot_nullarguments.test_upsert_omitted_not_null_with_check_constraint: validates tables withCHECKconstraints on omitted columns.black,flake8,mypy,pyright,ty, andpytest --sqlite-autocommit.📚 Documentation preview 📚: https://sqlite-utils--883.org.readthedocs.build/en/883/