Repository navigation
One encoding for primary key values in row URLs and pagination cursors - #3112
Merged
Merged
Conversation
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 Report❌ Patch coverage is 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. 🚀 New features to boost your workflow:
|
This was referenced Nov 22, 2026
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>
simonw
added a commit
that referenced
this pull request
Nov 23, 2026
Refs #1048, #1342, #1479, #1553, #1611, #1627, #1630, #1671, #1674, #1822, #1841, #1873, #1914, #1919, #1973, #2045, #2073, #2145, #2253, #2356, #2419, #2713, #2805, #2807, #2810, #2826, #2942, #2967, #3070, #3074, #3075, #3078, #3079, #3081, #3082, #3083, #3084, #3085, #3087, #3088, #3089, #3090, #3091, #3092, #3093, #3097, #3098, #3099, #3100, #3101, #3102, #3103, #3104, #3105, #3106, #3107, #3108, #3109, #3111, #3112, #3113, #3115
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thei:,f:,b:and$nullmarkers 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
345integerortext/t/345(unchanged)''/t/$emptyx'0102'/t/b:01021/t/i:1'1'/t/11.5/t/f:0x1~2E8000000000000p0'i:1'/t/i~3A1/t/$nullis a 404Numbers 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
rowid, and a cursor for one ends with it:$null,3. Previously the cursor was the textNoneand the next page skipped rows.binary 0102rather than as a Python bytes repr.1, the text'1'and the float1.5each have their own row page.?_shape=objectreturns 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, so1and'1'are separate keys./t/i%3A1and/t/%24emptyredirect to the marker form rather than to tilde-encoded text.Things that could break existing code
primary_key_valuesin the row page context and row JSON are now the values decoded from the URL. A binary key isbytes(a base64 object in JSON) and a typed number is anintorfloat. Ordinary keys are still strings. A customrow.htmlthat joins them as strings needs the newkey_value_displaytemplate filter.tilde_encode()will produce the wrong path for the keys listed above. Passing a list of values todatasette.urls.row()uses the new encoding, without column types."path"key on each row: the last part of the URL to that row's page, ornullfor a null key. The edit dialog uses it for its link to the referenced row.data-row-displayattribute holding the key as readable text. Custom templates without it fall back to decodingdata-row.i%3A5now means the integer 5. The texti:5is reached asi~3A5.f:0x1~2E8p0where 1.0a43 wrotef:0x1.8p0. The dot has to be escaped in a row URL, where it would start a.jsonstyle extension, and cursors use the same encoder.The null key check
To know whether
rowidis needed in theorder by, a table page runsselect 1 from t where pk is null limit 1. It is skipped for a singleinteger primary key, for keys declarednot 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
rowidshadows 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.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=objectand the once-per-request check. Codec unit tests are intests/test_utils.py.🤖 Generated with Claude Code
https://claude.ai/code/session_016BvfqDra1ayZvE9LSDbxvF