Skip to content

1.0a45

Pre-release
Pre-release

Choose a tag to compare

@simonw simonw released this 01 Dec 00:04
· 1 commit to main since this release

Many-to-many relationships

Datasette now displays many-to-many relationships on table and row pages, and provides tools for manipulating those relationships while editing a row. #3133

  • Pills for a junction table with extra columns are split in two: the related row on the left, and on the right a link to the junction table row showing the values of its first two text or number columns, such as a role, followed by "+N" for any others, with every value in its tooltip. See Pills.
  • Links can be added, removed and changed through plain junction tables and junction tables with extra columns (How relationships are detected). Unless they are configured, the links of relationships through repeatable junction tables, such as a reviews table with columns of its own, cannot be changed from the rows they link, only by editing the junction table's own rows, and these relationships are only shown if their foreign key is indexed.
  • New m2m table configuration key, for turning relationships off for a table, or choosing which ones to show, how to label them and which extra columns their pills show. The links of configured relationships can be edited from the rows they link whatever the kind of their junction table. See m2m.
  • New ?_m2m_size=, ?_m2m_truncate=0 and ?_nom2m=1 table and row arguments, for changing how many related rows are shown, showing extra column values in full and turning relationships off. See Special table arguments.
  • New ?_m2m.TABLE=value table filter, with __not, __in and __notin lookups, which filters a table by its rows' related rows through the junction table TABLE. The table page filter form shows these filters and can add them, searching the related table by label. See ?_m2m.TABLE=value and Filter and facet parameters.
  • New ?_facet_m2m=TABLE facet type, and m2m facets in configuration, which facet a table by its related rows. Datasette suggests these facets for tables that have a junction table. See Facet by many-to-many relationship.
  • The row edit and insert row dialogs can add and remove links, set and change the values of their extra columns, and create a related row from a label. The row and all of its link changes are saved in one transaction. See Editing links in the row dialogs.
  • The delete row dialog says what links to the row and what references it, and can delete its links and clear the foreign keys of rows that reference it, in the same transaction as the row. See Deleting rows.
  • The create table and alter table dialogs have a "Relationships" section, for creating relationships, which creates a junction table, and removing them, which drops it. See Creating and removing relationships.
  • New ?_extra=m2m and ?_extra=m2m_through extras for table and row JSON, and a ?_extra=foreign_key_details row extra. See Many-to-many relationships.
  • New APIs: POST /<database>/<table>/<row-pks>/-/m2m/<relationship> for adding, removing and changing links (Adding and removing many-to-many links), and GET /<database>/<table>/-/m2mfor listing a table's relationships (Listing a table's many-to-many relationships).
  • New options for existing write APIs: an m2m key for the row update, row insert and create tableAPIs, add_m2m and drop_m2m operations for the alter table API, which do not need alter-table on their own, and delete_m2m and clear_references keys for the row delete API.
  • Custom table templates get m2m_columns and row.m2m_cells, and row templates get m2m_sections. Relationship columns have the CSS classes m2m-col and m2m-col-NAME and a data-m2m="NAME" attribute. See Custom templates.

Breaking changes

  • Backwards incompatible: the ?_through={json} table filter has been removed, replaced by ?_m2m.TABLE.COLUMN=value. Unlike ?_through=, ?_m2m. only matches against foreign key columns. For now table pages redirect ?_through= to the equivalent ?_m2m. URL, in every format, and return a 400 error for a ?_through= that cannot be translated. The redirect will be removed in a later release. See the upgrade guide. (#621)
  • Backwards incompatible: the drop table API now only drops the table if confirm is the JSON boolean true. Previously any truthy value, such as "false", "no" or 1, dropped the table. Clients that send something other than a boolean now get a 400 error, confirm must be true or false, and the table is not dropped; false or no confirm still returns the preview.
  • Backwards incompatible: a request to the row delete API sent as application/json must now have a valid JSON body, otherwise it returns a 400 error. A request with no body, or a body of another content type, deletes the row as before.

Other features

  • The /-/jump endpoint has a new ?database= parameter, for returning only the items that belong to a database, such as its tables.
  • New ?_extra=row_paths table extra: the path of each row in rows, as used in its row page URL, in the same order. The insert and upsert APIs return the same row_paths list alongside rows when rows are returned.
  • The foreign_key_tables row extra, used for the "Referenced by" section of the row page, stops each count at 10,000, with a new count_capped key that is true if there are more, and counts under the facet_time_limit_ms time limit. If counting takes too long every count is null, rather than the list being empty.
  • Arbitrary SQL queries can now use explain and explain query plan with insert, update, delete and replace statements.
  • Datasette now depends on sqlite-utils>=4.2.2. With earlier versions the alter table API could lose a table's UNIQUE constraints, CHECK constraints and column collations, and made a nullable column NOT NULL when asked to keep it nullable, and the upsert API could overwrite values with NULL when rows in one request had different keys.

Bug fixes

  • Renaming a table using the alter table API or the alter table dialog now updates the foreign keys in other tables that reference it on every build of SQLite 3.26.0 or later. Previously those foreign keys were left pointing at the old name on builds where PRAGMA legacy_alter_table is on by default, including the SQLite that ships with macOS.
  • A rename-table event is no longer recorded when a single write drops one table and creates or rebuilds another. The new table can be given the storage that the dropped table had, which looked like a rename. A rename is now only reported if the table has the same definition before and after.
  • The schema pages, /<database>/-/schema and /<database>/<table>/-/schema with their .jsonand .md versions, find databases and tables whose names are tilde encoded in the URL, such as a database called release.v2 or a table name containing a / or a .. They returned a 404 for those databases and tables.
  • The foreign key targets endpoint used by the create and alter table dialogs no longer fails for a database with a table or view named pragma_table_info.
  • The list of suggestions shown by Datasette's autocomplete fields is attached to its input, so it is placed correctly with the on-screen keyboard open in iOS Safari. A search response that arrives after a suggestion was chosen, or the box was closed or emptied, is ignored.
  • Table pages no longer leave rows with a null primary key out of the count of rows before the current page, as used by ?_count=before, when the check for null primary keys runs out of time on a large table.
  • Long breadcrumbs in the header now wrap at word boundaries, including after each underscore in a database or table name, instead of breaking in the middle of a word. (#3128)