Skip to content

One encoding for primary key values in row URLs and pagination cursors - #3112

Merged
simonw merged 7 commits into
mainfrom
claude/funny-thompson-huk3kp
Nov 22, 2026
Merged

simonw merged 7 commits into
mainfrom
claude/funny-thompson-huk3kp

Conversation

@simonw

@simonw simonw commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Row URLs and the primary key part of ?_next= cursors encoded every value as tilde-encoded text. That could not represent a null, an empty string, a binary value, or a number in a column with no declared type (where SQLite does not convert text back to a number). Each of those broke row links, row pages, edit and delete, or pagination.

This adds one codec, encode_key_value() / decode_key_value(), and uses it everywhere a row is identified. It reuses the i:, f:, b: and $null markers that sort values in cursors have used since 1.0a43, and adds $empty.

Closes #2145, closes #2805, closes #2419, closes #2967

Demo: https://lite.datasette.io/?ref=85d9200&sql=https://gist.github.com/simonw/8bb66415344ba641ebc4d84ae543a27f#/

Claude Fable 5.2 Medium

What a key looks like

Key value Column Row URL
345 declared integer or text /t/345 (unchanged)
'' any /t/$empty
blob x'0102' any /t/b:0102
integer 1 no declared type /t/i:1
text '1' no declared type /t/1
float 1.5 no declared type /t/f:0x1~2E8000000000000p0
text 'i:1' any /t/i~3A1
null any none - /t/$null is a 404

Numbers only get a type in a column with no declared type (or declared blob), so existing row URLs and cursors are unchanged. Tilde-encoding never emits a raw $ or :, so a marker cannot be mistaken for text. Compound key links from before $empty, such as /t/x,, and the float form 1.0a43 wrote in cursors are still accepted.

Behaviour changes

  • Null primary keys. These rows have a blank key cell, no row link, no edit or delete buttons and no row page. SQLite allows several rows to share a null key, so there is nothing to identify one by.
  • Pagination past null keys. Rows with null keys are ordered by rowid, and a cursor for one ends with it: $null,3. Previously the cursor was the text None and the next page skipped rows.
  • Empty string keys have a row page, a visible empty link, and working edit and delete.
  • Binary keys have a row page, and are displayed as binary 0102 rather than as a Python bytes repr.
  • Untyped key columns. The integer 1, the text '1' and the float 1.5 each have their own row page.
  • ?_shape=object returns a 400 if any row has a null primary key, where it used to merge those rows under one key. Its keys are the same typed row paths, so 1 and '1' are separate keys.
  • Foreign key links on the table and row pages use the declared type of the column they reference.
  • Percent-encoded markers. /t/i%3A1 and /t/%24empty redirect to the marker form rather than to tilde-encoded text.

Things that could break existing code

  • primary_key_values in the row page context and row JSON are now the values decoded from the URL. A binary key is bytes (a base64 object in JSON) and a typed number is an int or float. Ordinary keys are still strings. A custom row.html that joins them as strings needs the new key_value_display template filter.
  • Code that builds row URLs itself using tilde_encode() will produce the wrong path for the keys listed above. Passing a list of values to datasette.urls.row() uses the new encoding, without column types.
  • The autocomplete API has a new "path" key on each row: the last part of the URL to that row's page, or null for a null key. The edit dialog uses it for its link to the referenced row.
  • Table rows and the row page field list have a new data-row-display attribute holding the key as readable text. Custom templates without it fall back to decoding data-row.
  • A percent-encoded i%3A5 now means the integer 5. The text i:5 is reached as i~3A5.
  • Float sort values in cursors are written f:0x1~2E8p0 where 1.0a43 wrote f:0x1.8p0. The dot has to be escaped in a row URL, where it would start a .json style extension, and cursors use the same encoder.

The null key check

To know whether rowid is needed in the order by, a table page runs select 1 from t where pk is null limit 1. It is skipped for a single integer primary key, for keys declared not null, for views and for tables with no primary key. It is capped at 20ms, runs once per request (so a streamed export does not repeat it for every page), and a timeout counts as "no nulls". Tables with no null keys get the same page SQL as before.

Not included

  • A column literally named rowid shadows the real one. The existing pagination for tables with no primary key has this problem and the new tie-breaker inherits it. To be filed as its own issue.
  • Inserting a row with a float or binary key into an untyped table shows "Inserted row. Refresh the page to see it." rather than adding the row to the page.
  • Changelog and upgrade guide entries are not written yet.
  • No Playwright tests for the JavaScript changes. I checked edit, delete and the foreign key link by hand in Chromium across empty, integer, float and binary keys.
  • Ensure all created primary keys are not null #2807 (declare created primary keys not null) is separate.

#2965 and #2839 address parts of this and are superseded. #2968 fixes #2967 a different way; I have not read it.

Tests

New end-to-end tests are in tests/test_row_keys.py: every row link leads to its own row, pagination visits every row exactly once (unsorted, sorted both ways, sizes 1 to 3, with the count-before endpoint agreeing), update and delete by typed path, redirects, fragments, foreign key links, _shape=object and the once-per-request check. Codec unit tests are in tests/test_utils.py.

🤖 Generated with Claude Code

https://claude.ai/code/session_016BvfqDra1ayZvE9LSDbxvF

SQLite allows null primary keys in rowid tables, and allows several rows
to share one, so such a row cannot be uniquely identified. The table page
previously linked it to /db/table/None, a 404, and showed edit and delete
buttons that could not work.

Those rows now render with a blank primary key cell, no data-row
attribute and no row actions. Their binary cells link by content hash
rather than by row path, on the table page and in CSV exports. The
insert dialog no longer builds a row path from a null primary key.

Refs #2145, #2805

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016BvfqDra1ayZvE9LSDbxvF
…-huk3kp

# Conflicts:
#	datasette/views/table.py
Row URLs and the primary key part of ?_next= cursors encoded every value
as tilde-encoded text. That could not represent a null, an empty string,
a binary value, or a number in a column with no declared type, where
SQLite does not convert text back to a number.

encode_key_value() and decode_key_value() now handle all of them, using
the i:, f:, b: and $null markers that sort values in cursors already
used, plus $empty. Numbers only get a type in columns with no declared
type, so existing row URLs and cursors are unchanged.

- Rows with an empty string primary key have a row page at /db/table/$empty
  and working edit and delete buttons
- Rows with a binary primary key have a row page
- Integers, floats and text with the same value in an untyped primary
  key column each have their own row page
- Pagination no longer stops or skips rows at a null primary key: rows
  with null keys are ordered by rowid, which the cursor then ends with
- /-/fragment?_row= selects the row using the decoded values
- Percent-encoded : and $ in a row path redirect to the marker, not to
  tilde-encoded text

Refs #2145, #2805, #2419, #2967

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016BvfqDra1ayZvE9LSDbxvF
…or binary keys

- ?_shape=object returns a 400 if any row has a null primary key, instead
  of merging those rows under one key
- primary_key_values on the row page are the values decoded from the URL:
  a binary key is bytes and a typed number is an int or float, where they
  were previously always strings. Templates can show one with the new
  key_value_display filter
- A binary primary key is displayed as 'binary 0102' in row links, row
  page headings, labels and messages, not as a Python bytes repr. Rows
  carry that text in data-row-display, which the edit and delete dialogs
  now use for their titles
- The check for null primary keys runs once per request, so a streamed
  export no longer repeats it for every page

Refs #2145, #2805, #2419

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016BvfqDra1ayZvE9LSDbxvF
Three more places built row paths as tilde-encoded text, so they pointed
at the wrong row or at nothing when the key was an empty string, a
binary value or a number in a column with no declared type:

- Foreign key cells on the table and row pages now link using the
  declared type of the column they reference, and show a binary value
  as hex
- The autocomplete API returns a "path" for each row, the last part of
  the URL to that row's page, or null for a row with a null primary key.
  The edit dialog uses it for its link to the referenced row
- ?_shape=object keys use the same typed row paths, so the integer 1 and
  the text "1" in an untyped primary key column are separate keys

CSV and TSV exports needed no change: they link to binary values in
tables with a primary key by content hash.

Refs #2145, #2805, #2419

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016BvfqDra1ayZvE9LSDbxvF
The 'empty' link picked up the rule for foreign key ids and similar
asides, which made it small, grey and upright. It is now italic and
otherwise matches the other primary key links.

Refs #2805

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016BvfqDra1ayZvE9LSDbxvF
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 203 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (9f2da6b) to head (85d9200).

Files with missing lines Patch % Lines
datasette/views/table.py 0.00% 125 Missing ⚠️
datasette/utils/__init__.py 0.00% 59 Missing ⚠️
datasette/renderer.py 0.00% 7 Missing ⚠️
datasette/app.py 0.00% 6 Missing ⚠️
datasette/csv_renderer.py 0.00% 2 Missing ⚠️
datasette/views/row.py 0.00% 2 Missing ⚠️
datasette/url_builder.py 0.00% 1 Missing ⚠️
datasette/views/query_helpers.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff           @@
##            main   #3112    +/-   ##
======================================
  Coverage   0.00%   0.00%            
======================================
  Files         81      81            
  Lines      14582   14707   +125     
======================================
- Misses     14582   14707   +125     

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@simonw
simonw merged commit fecd0c2 into main Nov 22, 2026
16 checks passed
@simonw
simonw deleted the claude/funny-thompson-huk3kp branch November 22, 2026 22:45
simonw added a commit that referenced this pull request Nov 23, 2026
…upgrade guide sections

Release notes for the null, empty string and binary primary key work
(#3112), the SpatiaLite KNN count fix (#1611), the insert API fixes
(#3081) and the first column facet on views (#2810), which landed
without any. The summary paragraph now mentions primary keys and view
filters.

New upgrade guide sections for datasette.urls.modify(), keyword-only
arguments, format_bytes(), TestClient and primary key values, linked
from their changelog entries.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants