Repository navigation
Ensure primary keys created via the JSON API are NOT NULL - #2826
Conversation
The table-create JSON API built its own CREATE TABLE without marking primary key columns NOT NULL, so a text or composite primary key could be created nullable. SQLite then allows rows with a NULL primary key, which cannot be viewed, edited or deleted. Force primary key columns NOT NULL, leaving a lone integer primary key alone since it aliases the rowid and is never NULL. Refs simonw#2807 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
@simonw lemme know when you can throw some eyes here or require any other changes |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3956e63650
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
SQLite resolves identifiers case-insensitively, so a request with pk "code" against a column named "Code" still creates that column as the primary key. The membership check was case-sensitive, so that column was left out of not_null and the API could still create a nullable text primary key, which is the exact unviewable/undeletable NULL-PK row this change is meant to prevent. The integer-rowid-alias exemption is matched the same way, so an integer pk whose casing differs still stays a plain rowid alias rather than picking up a constraint it does not need. Two cases added to test_create_table_primary_keys_are_not_null; the text one fails without this change (notnull 0, expected 1).
|
status check rather than a ping, since this has been sitting a month and the tree has moved a lot since I opened it. re-verified against current one thing that did change after I opened it, which is the part worth a second look: the first version only matched primary key names case-sensitively. the review bot caught that SQLite resolves identifiers case-insensitively, so happy to split the case-insensitivity part into its own commit if that reads better in review, or to rebase whenever you want it current. |
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The create table API can also create a table from example rows. That path still created text and compound primary keys that allowed nulls. A new table with those keys is now created before the rows are inserted, using column types taken from every row, so the primary key columns can be NOT NULL. A single integer primary key is left alone, including the one sqlite-utils adds when no row has that key. Existing tables are not changed. Both paths now share a _not_null_primary_keys() helper. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2826 +/- ##
=====================================
Coverage 0.00% 0.00%
=====================================
Files 81 81
Lines 14762 14776 +14
=====================================
- Misses 14762 14776 +14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
What was broken
The table-create JSON API (
/db/-/create) builds its ownCREATE TABLE(the explicit-columns path inTableCreateView.create_table) without marking primary key columnsNOT NULL. So a text or composite primary key is created nullable:→
CREATE TABLE "t" ("code" TEXT PRIMARY KEY, ...)—codecan beNULL.Why it matters
SQLite permits a
NULLvalue in any primary key that isn't a single-columnINTEGER PRIMARY KEY(and lets multiple rows share theNULLkey). As #2805 shows, such rows can't be viewed, edited or deleted through Datasette — the row URL resolves toNone/"". #2807 asks to "stem the bleeding" by disallowing null primary keys for tables created via the JSON API.What the fix does
In the explicit-columns create path — the one Simon described as "any time we run our own CREATE TABLE we set not null on any primary key we create" — primary key columns are added to the
not_nullset passed totable.create(). A single-columnINTEGER PRIMARY KEYis skipped because it aliases the rowid and can never beNULL, so no redundant constraint is emitted (and the commonidschema is unchanged).Result:
"code" TEXT PRIMARY KEY NOT NULL; composite keys getNOT NULLon every key column.What it deliberately does not change
INTEGER PRIMARY KEYoutput is untouched (stillINTEGER PRIMARY KEY), so existing schemas/tests/docs are unaffected.rows/insert_allinference path is not changed here. That path can also create a nullable text pk, but because its column types are inferred rather than declared, the single-integer-pk (rowid) exception can't be applied reliably in the same way. Happy to follow up on that path if you'd like it covered too — henceRefs #2807rather thanFixes.Testing
test_create_table_primary_keys_are_not_nullcovers a single text pk and a composite pk (schema +pragma_table_infonotnull), and asserts non-pk columns stay nullable.tests/test_api_write.py(148 passed), plustests/test_schema_endpoints.pyandtests/test_api.py, all green;black --checkandruff checkclean.Refs #2807
Developed with Claude Code; reviewed and tested by Pranav before marking ready for review.