datasette/datasette
Simon Willison f0645c6ddf
Create table, alter table - APIs and modal dialog UI
* Add create table UI

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 alter table JSON API

- 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

* Add alter table modal

- 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

* Ran Prettier

* Isolate Unix domain socket test server paths

- 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.

* Precompute action permissions for table pages

- 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.

* Test against pyodide/v314.0.0

Now that we depend on pydantic we need a more recent
pyodide in order to load the emscripten build
of pydantic-core.

Refs https://github.com/simonw/datasette/pull/2789#issuecomment-4733412763

* Split table create and alter views

- 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 foreign keys to create table API

- 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 https://github.com/simonw/datasette/pull/2789#issuecomment-4733544452

* Add foreign keys to alter table API

- 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.

* /db/table/-/foreign-key-suggestions API

Improved version of the implementation datasette-edit-schema

* /<database>/-/foreign-key-targets API endpoint

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.

* Expose foreign key targets to create table UI

- 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 foreign key controls to create table dialog

- 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

* More robust test_datasette_https_server.sh test

* Make custom type and foreign key mutually exclusive

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.

* Drop table button in alter dialog

* Object not chain of ifs

Refs https://github.com/simonw/datasette/pull/2789/changes#r3453964430

* Fix broken Playwright tests

* Keyword arguments for readability

* Ran prettier

* Removed the alter table dry run feature

It works by doing conn.backup(memory_conn) which could use
a lot of memory for a large database.

* sqlite-utils>=3.30,<4.0

So we don't get test failures from reformatted SQL.

* not_null, default and default_exr support for create table API columns

* Alter table API can now rename tables, refs #2788

Refs https://github.com/simonw/datasette/pull/2789#issuecomment-4771774289

* Fix for Safari select box heights

Refs https://github.com/simonw/datasette/pull/2789#issuecomment-4772241681

* Expose foreign key data to alter table UI

Include current foreign key metadata in the alter table page data and allow the foreign-key-targets endpoint to be read by actors with alter-table permission for a specific table.

Add API and HTML data tests for the new alter-table foreign key support.

* Unify create and alter table modal controls

Share default value controls between the create and alter table dialogs and expose create-table default expressions to the frontend.

Add create-table not-null/default handling and align the shared foreign key picker behavior across both dialogs.

* Add rename table controls to alter table dialog

Add a collapsed rename-table section to the alter table modal and include rename_table operations in the review/apply flow.

Redirect to the renamed table URL after applying changes and cover the review text in Playwright.

* current_unixtime and current_unixtime_ms default_expr options

Plus tweaked how alter table changing those works a bit.

* Draft changelog for create/alter table UI, refs #2787, #2788
2026-06-22 13:54:36 -07:00
..
default_permissions Disallow update/delete of private queries 2026-05-26 14:10:48 -07:00
publish Get publish cloudrun working with latest Cloud Run (#2581) 2025-11-05 09:28:41 -08:00
static current_unixtime and current_unixtime_ms default_expr options 2026-06-22 13:42:35 -07:00
templates Add create table UI 2026-06-22 10:11:56 -07:00
utils await request.json() method, closes #2767 2026-06-14 17:00:12 -07:00
views Create table, alter table - APIs and modal dialog UI 2026-06-22 13:54:36 -07:00
__init__.py register_token_handler() plugin hook for custom API token backends (#2650) 2026-02-25 16:32:45 -08:00
__main__.py Add support for running datasette as a module (#556) 2019-07-11 09:07:44 -07:00
_pytest_plugin.py Fix to our pytest plugin to better support pytest-cov 2026-06-11 07:29:27 -07:00
actor_auth_cookie.py Remove python-baseconv dependency, refs #1733, closes #1734 2022-05-02 12:39:06 -07:00
app.py /<database>/-/foreign-key-targets API endpoint 2026-06-22 10:11:56 -07:00
blob_renderer.py Rename route match groups for consistency, refs #1667, #1660 2022-03-19 09:52:08 -07:00
cli.py datasette inspect now counts 10,000+ tables correctly (#2752) 2026-05-28 15:52:51 -07:00
column_types.py Refine column field plugin API and documentation 2026-06-14 15:09:24 -07:00
csrf.py Normalize headers in CSRF checks, refs #2689 2026-04-14 19:24:38 -07:00
database.py Fix for gen.throw(*sys.exc_info()) warning 2026-06-13 11:15:47 -07:00
default_actions.py Rename canned queries to queries/stored queries in docs 2026-05-26 14:54:35 -07:00
default_column_types.py textarea column type 2026-06-13 22:18:45 -07:00
default_database_actions.py No execute-write on immutable databases 2026-05-25 12:46:21 -07:00
default_debug_menu.py Autocomplete widget and /-/debug/autocomplete test page 2026-06-13 22:59:37 -07:00
default_jump_items.py Remove source and source_key columns from JumpSQL 2026-05-23 20:41:32 -07:00
default_magic_parameters.py Fix datetime.utcnow deprecation warning 2024-03-15 15:32:12 -07:00
default_query_actions.py Web UI to edit and delete stored queries (#2764) 2026-06-08 20:19:47 -07:00
default_table_actions.py Add alter table modal 2026-06-22 10:11:56 -07:00
events.py RenameTableEvent, plus write connection track_event() mechanism (#2682) 2026-03-30 11:20:46 -07:00
extras.py Use asyncinject 0.7 results= seeding for per-request extras context 2026-06-11 06:42:08 -07:00
facets.py Renamed canned queries to queries / stored queries in docs 2026-05-26 15:17:51 -07:00
filters.py Fix type annotation bugs and remove unused imports 2025-10-26 16:03:13 -07:00
fixtures.py datasette.fixtures module, closes #2733 2026-05-21 23:05:37 -07:00
forbidden.py Fixed a bunch of unused imports spotted with ruff 2024-02-06 17:27:20 -08:00
handle_exception.py Remove navigation_search_js_hash mechanism 2026-05-23 09:09:07 -07:00
hookspecs.py Support for <button> items in action menus 2026-06-14 15:58:37 -07:00
inspect.py Black formatting 2026-02-17 13:30:24 -08:00
jump.py JumpSQL(database=) parameter 2026-05-23 21:00:04 -07:00
permissions.py Request-scoped permission check cache 2026-06-12 13:11:17 -07:00
plugins.py Add alter table modal 2026-06-22 10:11:56 -07:00
renderer.py Centralize JSON extra parsing 2026-06-08 20:45:01 -07:00
resources.py Rename canned queries to queries/stored queries in docs 2026-05-26 14:54:35 -07:00
sql_functions.py _search= queries now correctly escaped, fixes #651 2019-12-29 18:48:30 +00:00
stored_queries.py datasette.yml can no longer set a query to private 2026-05-28 15:37:48 -07:00
tokens.py TokenRestrictions.abbreviated(datasette) utility method for creating _r dicts (#2696) 2026-04-17 08:44:43 -07:00
tracer.py Fix for trace_child_tasks exception handling 2026-06-12 13:21:58 -07:00
url_builder.py count all rows button on table page, refs #2408 2024-08-21 19:09:25 -07:00
version.py Release 1.0a34 2026-06-16 14:27:26 -07:00
write_sql.py Allow SQL functions in SQL write queries 2026-05-28 10:22:28 -07:00