Skip to content

Handle ignored inserts with explicit primary keys#756

Open
KairosOps wants to merge 1 commit into
simonw:mainfrom
KairosOps:fix/ignored-insert-last-pk
Open

Handle ignored inserts with explicit primary keys#756
KairosOps wants to merge 1 commit into
simonw:mainfrom
KairosOps:fix/ignored-insert-last-pk

Conversation

@KairosOps

@KairosOps KairosOps commented Jun 21, 2026

Copy link
Copy Markdown

Summary

This fixes the IndexError from #554 when a single-row insert(..., pk=..., ignore=True) is ignored after another table has advanced SQLite's connection-level lastrowid.

In that case cursor.lastrowid can point at a rowid from a different table, so looking up that rowid in the current table can return no rows. The insert already had an explicit primary key, so when SQLite reports rowcount == 0 for the ignored insert this now keeps last_pk based on the input record instead of trying to resolve it through the stale rowid.

Checks

  • python -m pytest tests/test_create.py::test_insert_ignore tests/test_create.py::test_insert_ignore_with_pk_after_other_table_insert -q
  • python -m pytest tests/test_create.py -q
  • python -m py_compile sqlite_utils/db.py tests/test_create.py
  • git diff --check
  • python -m black --fast --check sqlite_utils/db.py tests/test_create.py

Refs #554


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

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.

1 participant