Skip to content

Use RETURNING to report single-insert primary keys - #958

Open
businessdeveloper69 wants to merge 1 commit into
simonw:mainfrom
businessdeveloper69:fix/return-inserted-primary-key
Open

businessdeveloper69 wants to merge 1 commit into
simonw:mainfrom
businessdeveloper69:fix/return-inserted-primary-key

Conversation

@businessdeveloper69

@businessdeveloper69 businessdeveloper69 commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes #359.

A single insert into a WITHOUT ROWID table can return last_pk == 0 or raise OperationalError: no such column: rowid after the row has already committed. Which result you get depends on whether an earlier insert on the same connection set a rowid.

This uses INSERT … RETURNING on SQLite 3.35+ to get the inserted key when supported. It returns single and composite keys from WITHOUT ROWID tables and leaves last_rowid unset. Ordinary tables with triggers retain their post-insert lookup. For WITHOUT ROWID, the docs explain that the returned key precedes any changes made by an AFTER trigger. Older SQLite versions keep the existing path.

Four on-disk reproductions covered fresh connections and connections with a prior rowid, each with single and composite keys. In every case the returned key retrieved the stored row. A SQL trace on an existing ordinary table showed six statements before and after for the default rowid case, and thirteen before versus eleven after for an explicit text key.

The full Python 3.12 and Python 3.14 autocommit runs each passed 1,510 tests with 13 skips. A real SQLite 3.23.1 compatibility run passed 252 with 10 skips. Formatting, flake8, mypy, Pyright, documentation and CLI checks passed. ty 0.0.107 reported the same redundant cast in existing list-mode code on both the upstream base and this commit.

AI assisted.


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use RETURNING if available to populate last_pk

1 participant