next token dropped when a row's only primary key value is the empty string
path_from_row_pks() returns "" for a row whose only primary key value is the empty string, and "" is a legitimate _next= cursor. But the next key and the _next= handling both test it for truthiness, so the token is silently discarded and pagination never advances.
>>> from datasette.utils import path_from_row_pks
>>> path_from_row_pks({"id": ""}, ["id"], False)
''What happens
datasette/views/table.py builds the response as:
"next": next_value and str(next_value) or None,next_value and str(next_value) or None is the x and y or z idiom, and "" and "" or None is None. So the JSON gets "next": null while next_url is still built unconditionally a few lines earlier and stays non-null. docs/json_api.rst:99 says:
"next", an opaque token that can be used to retrieve the next page using?_next=TOKEN, and"next_url", the full URL of that next page. Both arenullon the final page.
On the way back in, the same value is tested for truthiness again:
_next = _next or request.args.get("_next")
offset = ""
if _next:Result: a client that follows next_url loops on the first page
CREATE TABLE t (id TEXT PRIMARY KEY);
INSERT INTO t (id) VALUES ('a'), (''), ('c'), ('d'), ('e');/d/t.json?_size=1 -> rows [''] next=None next_url=/d/t.json?_size=1&_next=
following that URL -> rows [''] next=None next_url=/d/t.json?_size=1&_next=
following that URL -> rows [''] next=None next_url=...Never reaches 'a', let alone 'e'. The existing assert count < 30, "Possible infinite loop detected" guard in test_paginate_tables_and_views would not catch it, because this loops forever rather than 30 times.
The one place a test asserts next_url and next agree is already correct and already fails here:
# tests/test_table_api.py:333
assert urllib.parse.urlencode({"_next": response.json()["next"]}) in path
# AssertionError: assert '_next=None' in 'http://localhost/empty_pk/t.json?_size=1&_next='It passes today only because every stock fixture has non-empty primary keys.
The two sites
# emit
"next": None if next_value is not None else str(next_value),
# consume
_next = _next if _next is not None else request.args.get("_next")
if _next is not None:None is the "no cursor" sentinel — it is initialised to None at table.py:2508 and nothing ever assigns "" to it deliberately. The same file already asks this question correctly four times nearby, including once in this very function: if prefix is None: prefix = "$null" at table.py:2536, and if value is None or value == "": at table.py:287.
One thing I am unsure about, and would rather you decided
This needs a table whose single text primary key holds ''. That is legal SQLite and nothing in Datasette rejects it, but I could not find a path to it that does not involve someone importing a CSV with an empty key, so it may be rare enough that you would rather not carry the change. What convinced me it is a bug rather than an unsupported edge case is that the observable output contradicts json_api.rst — a non-null next_url next to a null next is not something the documented contract allows, whatever the input.
A bare ?_next= with no value does change meaning: it used to be ignored and return page 1, and now it means "rows after the empty primary key", which is the only coherent reading of an empty cursor. _next=a and _next=zzz behave as before.
No documentation change is needed — the docs already describe the fixed behaviour, the code was the thing that disagreed.
Source: simonw/datasette