Skip to content

Fix rowid pk and last_rowid regressions in insert/upsert#782

Merged
simonw merged 3 commits into
mainfrom
claude/datasette-tests-mown-2uaqtt
Jul 7, 2026
Merged

Fix rowid pk and last_rowid regressions in insert/upsert#782
simonw merged 3 commits into
mainfrom
claude/datasette-tests-mown-2uaqtt

Conversation

@simonw

@simonw simonw commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Refs:

Two behaviour regressions in the 4.0 insert/upsert rewrite broke callers
(notably Datasette's write API) that operate on tables without an explicit
primary key. Both are fixed here with regression tests.

  1. rowid (and its aliases rowid/oid) were rejected as a primary key.
    Table.pks already reports ["rowid"] for a rowid table, but the new pk
    validation raised InvalidColumns because rowid is not listed among the
    table's columns, and the insert success path then raised KeyError when
    looking up the pk value. rowid aliases are now accepted for rowid tables
    and resolve directly to the rowid.

  2. An ignored insert (INSERT OR IGNORE that matched an existing row) no
    longer populated last_rowid, and only set last_pk when an explicit pk=
    was passed. It now locates the existing conflicting row by its primary
    key values and reports that row's rowid and pk, rather than relying on
    the connection's last inserted rowid.

Add a shared ROWID_ALIASES constant for the rowid alias names.

Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01E7af8SxFZqiCerJB6MqKnY


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

claude added 2 commits July 7, 2026 14:57
Two behaviour regressions in the 4.0 insert/upsert rewrite broke callers
(notably Datasette's write API) that operate on tables without an explicit
primary key. Both are fixed here with regression tests.

1. rowid (and its aliases _rowid_/oid) were rejected as a primary key.
   Table.pks already reports ["rowid"] for a rowid table, but the new pk
   validation raised InvalidColumns because rowid is not listed among the
   table's columns, and the insert success path then raised KeyError when
   looking up the pk value. rowid aliases are now accepted for rowid tables
   and resolve directly to the rowid.

2. An ignored insert (INSERT OR IGNORE that matched an existing row) no
   longer populated last_rowid, and only set last_pk when an explicit pk=
   was passed. It now locates the existing conflicting row by its primary
   key values and reports that row's rowid and pk, rather than relying on
   the connection's last inserted rowid.

Add a shared ROWID_ALIASES constant for the rowid alias names.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7af8SxFZqiCerJB6MqKnY
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7af8SxFZqiCerJB6MqKnY
@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.46%. Comparing base (d314d04) to head (d53e3cb).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #782      +/-   ##
==========================================
+ Coverage   95.15%   95.46%   +0.31%     
==========================================
  Files           9        9              
  Lines        3712     3727      +15     
==========================================
+ Hits         3532     3558      +26     
+ Misses        180      169      -11     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Cover the remaining branches of the ignored-insert lookup that determines
last_pk and last_rowid for the existing conflicting row: compound primary
keys, list-based iteration, hash_id (pk is the computed hash, rowid cannot
be looked up), and the unresolvable cases where the conflict is on a UNIQUE
column rather than the primary key (last_pk and last_rowid left unset).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7af8SxFZqiCerJB6MqKnY
@simonw

simonw commented Jul 7, 2026

Copy link
Copy Markdown
Owner Author

That last commit is because I prompted:

The change in db.py is quite complex check if the tests fully cover that using coverage

@simonw
simonw merged commit 60811e7 into main Jul 7, 2026
106 checks passed
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.

2 participants