Repository navigation
1.0a45
Pre-release
Pre-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
reviewstable 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
m2mtable 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=0and?_nom2m=1table 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=valuetable filter, with__not,__inand__notinlookups, which filters a table by its rows' related rows through the junction tableTABLE. 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=TABLEfacet type, andm2mfacets 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=m2mand?_extra=m2m_throughextras for table and row JSON, and a?_extra=foreign_key_detailsrow 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), andGET /<database>/<table>/-/m2mfor listing a table's relationships (Listing a table's many-to-many relationships). - New options for existing write APIs: an
m2mkey for the row update, row insert and create tableAPIs,add_m2manddrop_m2moperations for the alter table API, which do not needalter-tableon their own, anddelete_m2mandclear_referenceskeys for the row delete API. - Custom table templates get
m2m_columnsandrow.m2m_cells, and row templates getm2m_sections. Relationship columns have the CSS classesm2m-colandm2m-col-NAMEand adata-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 a400error 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
confirmis the JSON booleantrue. Previously any truthy value, such as"false","no"or1, dropped the table. Clients that send something other than a boolean now get a400error,confirm must be true or false, and the table is not dropped;falseor noconfirmstill returns the preview. - Backwards incompatible: a request to the row delete API sent as
application/jsonmust now have a valid JSON body, otherwise it returns a400error. 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_pathstable extra: the path of each row inrows, as used in its row page URL, in the same order. The insert and upsert APIs return the samerow_pathslist alongsiderowswhen rows are returned. - The
foreign_key_tablesrow extra, used for the "Referenced by" section of the row page, stops eachcountat 10,000, with a newcount_cappedkey that istrueif there are more, and counts under the facet_time_limit_ms time limit. If counting takes too long everycountisnull, rather than the list being empty. - Arbitrary SQL queries can now use
explainandexplain query planwithinsert,update,deleteandreplacestatements. - Datasette now depends on
sqlite-utils>=4.2.2. With earlier versions the alter table API could lose a table'sUNIQUEconstraints,CHECKconstraints and column collations, and made a nullable columnNOT NULLwhen asked to keep it nullable, and the upsert API could overwrite values withNULLwhen 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_tableis on by default, including the SQLite that ships with macOS. - A
rename-tableevent 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>/-/schemaand/<database>/<table>/-/schemawith their.jsonand.mdversions, find databases and tables whose names are tilde encoded in the URL, such as a database calledrelease.v2or 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
nullprimary 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)