Create table, alter table - APIs and modals#2789
Conversation
Previously there were four lines of whitespace, but that meant users had to delete that whitespace themselves when they started editing. Now it is four lines tall without that.
Adds a permission-gated database action that opens a create table modal on database pages, backed by the existing create-table JSON API. The modal starts with an id integer primary key column plus a blank text column, supports SQLite type selection, and shows custom column type controls only when the actor can set column types. Selected custom column types are applied after table creation with follow-up set-column-type API calls. Includes styling plus HTML and Playwright coverage for the action payload and create-table flow.
- Add POST /<database>/<table>/-/alter with Pydantic validation and dry-run support. - Support add, rename, alter, drop, primary-key and reorder operations, including allow-listed default expressions. - Document the endpoint and cover schema changes, validation, permissions, events and dry runs. Refs #2788
- Register a built-in table action and expose alter-table metadata to table pages. - Build the client-side modal for editing columns, defaults, ordering, primary keys, and custom column types. - Add a review/apply confirmation flow with HTML and Playwright coverage. Refs #2788
| dialog.table-create-dialog { | ||
| --ink: #0f0f0f; | ||
| --paper: #eef6ff; | ||
| --muted: #6b6b6b; | ||
| --rule: #d8e6f5; | ||
| --accent: #1a56db; | ||
| --card: #ffffff; | ||
| border: none; | ||
| border-radius: var(--modal-border-radius, 0.75rem); | ||
| padding: 0; | ||
| margin: auto; | ||
| width: min(760px, calc(100vw - 32px)); | ||
| max-width: 95vw; | ||
| max-height: min(780px, calc(100vh - 32px)); | ||
| box-shadow: var(--modal-shadow, 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04)); | ||
| animation: datasette-modal-slide-in var(--modal-animation-duration, 0.2s) ease-out; | ||
| overflow: hidden; | ||
| font-family: system-ui, -apple-system, sans-serif; | ||
| background: var(--card); | ||
| } | ||
|
|
||
| dialog.table-create-dialog[open] { | ||
| display: flex; | ||
| flex-direction: column; | ||
| } |
There was a problem hiding this comment.
This is consistent with our other dialogs but there's a whole lot of duplicate model code now. Filing an issue to clean that up later:
- Use a per-process socket path for the UDS test fixture. - Clean up stale socket files before and after the fixture runs. - Close the HTTP client and wait for the Datasette subprocess to exit.
- Extract reusable helpers for database and table action permission preloading. - Precompute those permissions before building table-page HTML data. - Document the default table actions plugin.
|
Pyodide tests are failing with:
That shouldn't be an issue now, see: https://simonwillison.net/2026/Jun/13/publishing-wasm-wheels/ Might need to upgrade Pyodide (here and in Datasette Lite). |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2789 +/- ##
======================================
Coverage 0.00% 0.00%
======================================
Files 70 72 +2
Lines 11173 11702 +529
======================================
- Misses 11173 11702 +529 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Now that we depend on pydantic we need a more recent pyodide in order to load the emscripten build of pydantic-core. Refs #2789 (comment)
|
There's a feature missing from both create and alter: foreign key constraints. These are worth supporting, particularly since the edit/insert modals have neat support for them. Related to that: setting the "label column" for a table. Might need a larger issue for that since we need to store label columns somewhere, probably in the same place as custom column types. |
- Move create-table and alter-table API views into table_create_alter.py. - Keep create and alter schema-editing constants and helpers together. - Rename the create table modal context helper.
- Add fk_table and optional fk_column support to create-table columns. - Validate create-table requests with Pydantic while preserving existing errors. - Document the API and cover inferred primary-key and validation cases. Refs #2789 (comment)
- Add add_foreign_key, drop_foreign_key, and set_foreign_keys operations. - Validate flat fk_table and fk_column arguments with Pydantic. - Document the API and cover inferred primary-key and validation cases.
2930f7a to
fbab41f
Compare
Improved version of the implementation datasette-edit-schema
Returns a list of tables with a single primary key, and for each one the name of that primary key column and its SQLite type affinity. This will be used by the create table UI to suggest foreign keys.
- Add foreignKeyTargetsPath to create table page data - Filter hidden tables from database-level foreign key target results - Update JSON API docs and tests for filtered targets
- Add create table advanced controls for foreign keys and first-column primary keys - Share schema dialog row helpers between create and alter dialogs - Move custom type into advanced options and add Add column icons
In the create table dialog a column can now have either a custom display type or a foreign key target, but not both - a foreign key column's type is determined by the referenced primary key, so a custom type doesn't apply. Setting one clears and disables the other, and the foreign key select stays disabled on the primary key column and when no targets exist. Also add "Controls how Datasette displays and edits this column" help text (with aria-describedby) under the custom type selector in both the create and alter dialogs, and style the alter dialog help text.
Refs:
📚 Documentation preview 📚: https://datasette--2789.org.readthedocs.build/en/2789/