Skip to content

Apply SQL conversions once during upsert updates - #947

Merged
simonw merged 2 commits into
simonw:mainfrom
sanidhyasaluja:fix/upsert-conversions-once
Nov 29, 2026
Merged

simonw merged 2 commits into
simonw:mainfrom
sanidhyasaluja:fix/upsert-conversions-once

Conversation

@sanidhyasaluja

@sanidhyasaluja sanidhyasaluja commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes:

When an upsert updates an existing row, SQL conversions are applied to the incoming value in VALUES and then again in DO UPDATE. For example, ? * 100 stores 120000 instead of 1200 for an incoming value of 12. JSON extraction can fail on the second conversion, and replacing every ? also changes question marks inside SQL string literals.

Assign excluded.<column> directly in the conflict-update clause, since it already contains the converted insertion value. This gives inserts and updates the same conversion-once behavior and leaves the legacy upsert implementation intact. The API documentation now states that behavior.

Regression coverage includes both upsert methods and implementations, multiplication, suffixes, JSON extraction, literal question marks, repeated updates, and mixed insert/update batches with two batch sizes.

Validation on Linux, Python 3.13.18, SQLite 3.53.1:

  • New regression cases against unchanged main: 10 failed, 10 passed; with the fix, all 20 pass.
  • Full suite: 1,517 passed, 16 skipped.
  • Conversion and upsert tests: 44 passed. Conversion tests with --sqlite-autocommit: 26 passed.
  • Black, flake8, mypy, generated-documentation consistency (cog --check --diff), and git diff --check pass.

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

Comment thread docs/python-api.rst Outdated

The dictionary key is the column name to be converted. The value is the SQL fragment to use, with a ``?`` placeholder for the original value.

Conversions are applied once to each incoming value. With ``upsert()`` and ``upsert_all()``, this is the same whether a row is inserted or updated.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this wording be limited to non-primary-key values, or mention the legacy exception? With use_old_upsert=True, a conversion on the primary key is currently ignored:

db = Database(memory=True, use_old_upsert=True)
table = db["records"]
table.upsert({"id": 3, "value": "a"}, pk="id", conversions={"id": "? * 10"})
list(table.rows)  # [{"id": 3, "value": "a"}]

The native implementation stores id=30. I checked both upsert() and upsert_all() against the base and this PR using in-memory SQLite. This difference predates the PR; I don't think the old path needs to be changed here, but “each incoming value” appears broader than the existing behaviour. Checked with Codex assistance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this. Confirmed for both upsert() and upsert_all(): the native path stores id=30, while the legacy path stores id=3, including on subsequent updates. I narrowed the documentation to non-primary-key values and explicitly noted the use_old_upsert=True primary-key exception in e16d89d. The existing legacy behavior is unchanged.

@simonw simonw added the bug Something isn't working label Nov 29, 2026
Comment thread tests/test_conversions.py
table = db["records"]
for value, expected in (
(first, expected_first),
(second, expected_second),

@simonw simonw Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This second operation was the one that previously exposed the bug - the conversion ran once in the VALUES clause and again in DO UPDATE SET.

@simonw
simonw merged commit cee9718 into simonw:main Nov 29, 2026
1 check passed
simonw added a commit that referenced this pull request Nov 29, 2026
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.

3 participants