Elyra
Elyra The coding agent eTerm The terminal that knows where each command ends Huskarl AI colleagues on your own Mac Starf An activity monitor for Apple silicon that never invents a number etrans An SSH and SFTP client for macOS Elyra VM Virtual machines for macOS, Linux and Windows on your Mac Litr A small, native web browser for macOS Notr A notebook for macOS e The native code editor Elyra Grove Native local development environment Askr The real server for Laravel & PHP Elyra Framework Rust + Svelte 5 framework for desktop apps Elyra Conductor Local project conductor Refr Local-first PDF workspace for macOS Elyra Workspace A desktop workspace for coding agents eDock A container runtime for the Mac Elyra SQL Server MySQL-compatible SQL server in Rust Elyra Félagi Agents as teammates on one board Elyra SQL Client Native desktop SQL workbench Elyra SQL Anywhere Replication-ready SQL engine Elyra Sjá SEO & GEO workspace for macOS Elyra DataGrid Server-driven data grid for Laravel
Reference
Ratatosk CLI
Roadmap
RFC 0001: make:resource
RFC 0002: live queries
RFC 0003: MCP server
RFC 0004: MCP resource templates
RFC 0005: a Laravel backend
RFC 0006: Laravel broadcasting
RFC 0007: offline and sync
RFC 0008: Laravel notifications
RFC 0009: Files
RFC 0010: Laravel abilities
RFC 0011: an assistant in the app
RFC 0012: @elyra/ui
RFC 0013: the assistant sees the window
RFC 0014: Elyra Inspector
RFC 0015: presence
RFC 0016: capabilities per window
RFC 0017: record invalidation
RFC 0018: the assistant's answer, streamed
RFC 0019: a window from the tray
RFC 0020: an app that stays in the tray
RFC 0021: a server chosen at sign-in
RFC 0022: signed in to more than one server
RFC 0023: private channels a window listens to
RFC 0024: updates from the backend
RFC 0025: commands and configuration
RFC 0026: the running app, for coding agents
RFC 0027: the assistant's code mode
RFC 0028: the tray's icon, and the app's
RFC 0029: installers
RFC 0030: the app's identity on Windows
RFC 0031: a token per workspace
RFC 0032: the microphone
RFC 0033: diagnostics the user can send
RFC 0034: one screen in several states at once
RFC 0035: point at it, and hand it to the agent
RFC 0036: rata check, the app checked as a whole
RFC 0037: Theme Studio
RFC 0038: edit it in its own app
RFC 0039: a write shown at once in every window
RFC 0040: the two halves agree
RFC 0041: Quick Look
RFC 0042: follow a colleague
RFC 0043: every write seen
RFC 0044: composite keys
RFC 0045: bigint all the way
RFC 0046: pivot columns
RFC 0047: relations on any key
RFC 0048: more providers
RFC 0049: OpenAI web search
RFC 0050: integer-keyed arguments
RFC 0051: citations
RFC 0052: open documents
RFC 0053: exit hooks
RFC 0054: the gui feature
Release notes
Changelog
Elyra
RFC 0044 — Composite keys: a model whose row is named by more than one column

RFC 0044 — Composite keys: a model whose row is named by more than one column

Status: implemented (2026-11-21) · Target: 0.51.0 · builds on models · see Decisions

Summary

A model's row is named by one column today: an i64 the database assigns, or one value the app supplies (#[model(id)] key: String). Plenty of tables are named by two: an order's lines (order_id, line_no), a user's settings per workspace (user_id, workspace_id, key), a pivot with columns of its own (role_user with granted_by and expires_at). The models page says they aren't supported; such a table is read with Query and written with raw SQL.

This RFC lets #[model(id)] mark more than one field:

#[derive(Model)]
#[model(table = "order_lines", timestamps)]
struct OrderLine {
    #[model(id)]
    order_id: i64,
    #[model(id)]
    line_no: i64,
    product: String,
    qty: i64,
}

let line = OrderLine::find(&db, (42, 3)).await?;          // the key, in field order
line.update(&db).await?;                                   // WHERE order_id = ? AND line_no = ?
line.delete(&db).await?;
OrderLine { order_id: 42, line_no: 4, ..line }.insert(&db).await?;
OrderLine::query().with_trashed().find(&db, (42, 3)).await?;
  • The key is a tuple of the key fields' types, in the order they're declared.
  • Every key is the app's: it's inserted as written and never read back, as a single app-supplied key is today.
  • save() writes the row whether or not it's there (an upsert on the key), since there's no "unsaved" value to tell.
  • What reads and writes a whole table (queries, pagination, soft deletes, timestamps, casts on the other fields, global scopes, factories, live queries) works as it does for any model.

Motivation

  • Real schemas have them. Laravel apps the desktop half meets (RFC 0005) and SQLite files an app inherits use composite keys for line items, per-scope settings, translations (post_id, locale) and pivots with data. Today each is a struct read through Query and written with sqlx::query, without update, delete or factories.
  • A pivot with columns (belongs_to_many with granted_by) is the common case: the relation attaches and detaches, and the pivot's own columns have no model to read or change them through.
  • It's the last "not supported" on the models page about keys; the roadmap lists it under Models.

Current state (0.50)

  • Model::PK is one column. The derive finds a field marked #[model(id)], or one whose column is id, and generates find(db, key), insert, update and delete (WHERE pk = ?), and save(), which inserts when the key is its type's Default (0, "") and updates otherwise.
  • Query::find(db, id: impl Into<Value>) adds WHERE table.pk = ?.
  • An i64 key is the database's (RETURNING, last_insert_id); any other is the app's, inserted as written.
  • Relations (has_many, has_one, belongs_to, belongs_to_many) key on i64 columns; eager loading groups by i64.
  • A second #[model(id)] is ignored: the first one wins, silently.

Design

Declaring it

Two or more fields marked #[model(id)] make the key composite, in declaration order. A cast field can't be one (as today). A composite key is always the app's: none of its columns is read back after an insert, even an i64 one, because the database assigns at most one autoincrement column, and then the key is that column alone.

The key

Model gains const KEY: &'static [&'static str], the key's columns (one for a single key, so code that walks it doesn't branch), and an associated type Key: the key field's type for a single key, a tuple for a composite one. PK stays the first key column, for code written against it.

The generated find(db, key: Self::Key) and Query::find(db, key) take the key; Query::find accepts anything Into<M::Key>, so find(&db, 42) and find(&db, "theme") still compile for the models that have them. Query::where_key(key) adds the key's WHERE for the builder's other uses (update, delete, soft_delete of one row).

Writing

  • insert writes every column, the key's included, as written.
  • update sets every column but the key's, WHERE each key column equals the instance's: changing a key field and calling update writes to the row the new key names, as for a single key today.
  • delete deletes WHERE the key.
  • save() is an upsert on the key: SQLite's and Postgres' INSERT … ON CONFLICT (key columns) DO UPDATE SET …, MySQL's INSERT … ON DUPLICATE KEY UPDATE …. With timestamps, created_at is set on insert only and updated_at on both. It needs the key columns to be the table's primary key or a unique index, which the migration says (t.primary(&["order_id", "line_no"])).

Relations

  • From a composite-key model, belongs_to works as it does from any model: it reads its own foreign-key column (OrderLine belongs to Order through order_id).
  • To one (has_many(OrderLine) from a model whose children are named by two columns) works too: it's the child's foreign key that matters, not its key.
  • A composite-key model as a parent of has_many, has_one or belongs_to_many is a compile error that says so: those relations key on one i64 column of the parent.
  • A pivot with columns is read and written through its own model (RoleUser keyed by role_id, user_id); belongs_to_many keeps attaching and detaching. Exposing the pivot's columns on the related models (Laravel's withPivot) isn't in this RFC.

What doesn't change

Single keys, i64 or the app's, generate exactly what they do now; PK keeps its meaning; live queries are keyed by table, so a composite key changes nothing there; codegen sees the struct's fields as before.

Testing

The model tests on SQLite, and model_servers.rs on MySQL and Postgres in CI: find, insert, update, delete, save both ways (new row and existing), with_trashed().find, timestamps on upsert, a factory, a belongs_to from the composite model. The compile errors (a cast key field; has_many from a composite parent) get unit tests in the derive, on the attribute parsing.

Implementation plan

  1. elyra-db: Model::KEY, type Key and its values, Query::find over Into<M::Key>, Query::where_key, the upsert per driver.
  2. The derive: more than one #[model(id)], the generated find, update, delete, save, the compile errors.
  3. Tests on all three drivers; the models page's "Primary keys" section and its "v1 assumptions".

Not in this RFC

  • withPivot: a pivot's columns on the models belongs_to_many returns.
  • make:resource for a composite-key model: its routes, views and commands assume one id.
  • A composite key as a relation's parent (has_many keyed on two columns).
  • first_or_create / update_or_create / upsert on the query builder, which the models page also lists as not done; save()'s upsert is the instance's, not a bulk one.

Alternatives considered

  • A key struct (OrderLineKey { order_id, line_no }) generated beside the model: names each part at the call site, but it's another type per model, and a tuple in declaration order reads well enough for two or three columns.
  • A surrogate id column: what's recommended for new tables, and no help for a schema the app didn't design.
  • save() not generated for a composite key (insert and update only): avoids the upsert, but save() is what factories and generated code call, and an upsert on the key is what "save this row" means when there's no unsaved value.

As built

  • elyra-db. Model gains const KEY and type Key: ModelKey, both generated by the derive (nothing else implements Model). ModelKey is implemented for any T: Into<Value> (one value) and for tuples of two to four; a composite key has at most four columns, a compile error past that. Query::find and the new Query::where_key take impl Into<M::Key>, so an i32 reaches an i64 key through From.
  • The derive. Two or more #[model(id)] fields make the key, in declaration order. update and delete bind every key column through the same argument list as the other columns (a bool key column is 0/1, as elsewhere); update with nothing but key columns sets nothing and returns. insert for a composite key writes every column and reads nothing back, even when a key column is i64.
  • save() is one statement: INSERT … ON CONFLICT (keys) DO UPDATE SET c = excluded.c, … on SQLite and Postgres, INSERT … ON DUPLICATE KEY UPDATE c = VALUES(c), … on MySQL (VALUES() works on MySQL 8 and MariaDB alike; the row alias is 8.0.19+ only). A table whose columns are all the key's gets DO NOTHING, or k = k on MySQL. With timestamps, created_at is set when it's 0 and left out of the update, so the row keeps its own; updated_at is set always.
  • Relations. A composite-key model as the parent of has_many, has_one or belongs_to_many (struct- or field-level) is a compile error naming why; belongs_to from it works.
  • Tested in framework/tests/model_keys.rs on SQLite (find, the builder's find and where_key, update and delete by the whole key, belongs_to, soft delete and with_trashed().find, save() both ways keeping created_at, an all-key table, a factory, an i32 for an i64 key) and in model_servers.rs on MySQL 8 and Postgres 16 (the same CRUD and both drivers' upserts). The compile errors are compile_fail doctests on elyra::composite_keys, not unit tests in the derive: a proc macro's output can't be expanded in its own tests.

Decisions

  1. The key is a tuple, in declaration order; a key struct can come later if call sites ask for names.
  2. save() is an upsert on the key, documented as needing a primary key or unique index on those columns.
  3. Query::find takes Into<M::Key> from 0.51, said under Upgrading: find(&db, 42) and find(&db, "theme") keep compiling; another type for the key (an i32 for an i64) doesn't.
  4. A second #[model(id)] makes the key composite, said under Upgrading; it was silently ignored before.