RFC 0047 — Relations on any key: a parent named by a string, or by two columns
Status: implemented (2026-11-21) · Target: 0.52.0 · builds on models, RFC 0044 · see Decisions
Summary
Relations key on one i64 column. A model named by anything else can't take
part in has_many, has_one or belongs_to:
- A composite-key parent (
order_lineskeyed byorder_idandline_no) is refused with a clear compile error (RFC 0044). - A single non-
i64key (teamskeyed bycode: String) isn't refused. It fails inside the generated code:has_manyfrom it with "cannot move out of a shared reference", andbelongs_toto it with "mismatched types". The models page says only that "relation eager-loading still assumesi64keys"; the lazy accessors don't compile either.
This RFC lets a relation key on the parent's key, whatever it is:
#[derive(Model)]
#[model(table = "teams", has_many(Member, fk = "team_code", as = "members"))]
struct Team { #[model(id)] code: String, name: String }
#[derive(Model)]
#[model(table = "members", belongs_to(Team, fk = "team_code"))]
struct Member { id: i64, team_code: String, name: String }
#[derive(Model)]
#[model(table = "order_lines", has_many(LineNote, fk = ("order_id", "line_no"), as = "notes"))]
struct OrderLine { #[model(id)] order_id: i64, #[model(id)] line_no: i64, qty: i64 }
#[derive(Model)]
#[model(table = "line_notes", belongs_to(OrderLine, fk = ("order_id", "line_no"), as = "line"))]
struct LineNote { id: i64, order_id: i64, line_no: i64, body: String }
team.members(&db).await?; // Vec<Member>
member.team(&db).await?; // Option<Team>
let by_line = OrderLine::load_notes(&db, &lines).await?; // HashMap<(i64, i64), Vec<LineNote>>
let notes = &by_line[&line.key()];
note.line(&db).await?; // Option<OrderLine>
fknames as many columns as the parent's key has, in the order of the parent's key: a string for one, a tuple of strings for more.load_*maps are keyed by the parent's key type (Model::Key). For ani64key that'sHashMap<i64, _>, exactly as today.Model::key(&self)returns a row's key, so a map is looked up withmap[&line.key()].belongs_to_manystill needs ani64parent and related key; its pivot holds integers.
Motivation
- Schemas the app didn't design: Laravel apps the desktop half meets
(RFC 0005), and SQLite files an app inherits, key on codes, slugs, UUIDs
and pairs. RFC 0044 gave such tables a model. Their relations still have
to be written as
Querycalls and joined by hand. - A compile error from inside generated code is a bug: the derive
accepts
has_manyon aString-keyed model and then produces code that doesn't compile, with no pointer to why. - The roadmap's Models line ends with "a composite-key model as a relation's parent". After RFC 0046 it's the last item there.
Current state (0.51)
has_manyandhas_one: the lazy accessor isC::query().where_eq(fk, self.pk).load_*goes througheager_has_many::<P, C>, which collects parent keys withget_i64(parent_pk)and groups the children byget_i64(fk).belongs_to: the lazy accessor reads the child's fk withget_i64and callsP::find(db, id).load_*goes througheager_belongs_to, keyed byi64.- Field relations (
with_*) fill the field from those maps, looked up byget_i64(PK). - A composite parent of
has_many,has_oneorbelongs_to_manyis a compile error. A non-i64single key isn't checked, and fails as above.
Design
Declaring it
fk = "col" as today, or fk = ("a", "b") with as many columns as the
parent's key, in the parent's key order. The macro checks what it can see:
- On
belongs_to, the fk columns are the declaring model's own, so each must be one of its fields (compile error otherwise). - On
has_manyandhas_one, the count must match the declaring model's key (compile error otherwise). The columns belong to the other model, so they're checked when the query runs, as today.
A composite key has no default fk: leaving fk out on a composite parent,
or on a belongs_to to a composite parent, is a compile error that asks for
it. A single key keeps today's default, {type}_id.
The key
Model gains fn key(&self) -> Self::Key, generated (the key fields,
cloned). The eager paths need Model::Key: Hash + Eq + Clone, and need it
decodable from a row through a new KeyFromRow trait. KeyFromRow is
implemented for i64, i32, String and bool, and for tuples of these
of 2 to 4 elements: the scalars the Any driver decodes. A key of another
type gets the lazy accessors and a compile error on load_* naming
KeyFromRow.
Reading
- Lazy, from the parent (
has_many,has_one): onewhere_eqper key column, with the parent's key fields cloned. This removes today's move error. - Lazy, from the child (
belongs_to): the child's fk fields, cloned into the parent'sKey, thenP::find.Optionfk fields areNone→Ok(None), as a missingi64fk is today. - Eager,
has_manyandhas_one: the parents' keys, then one query:where_infor one column, or a row-valueINfor more:(order_id, line_no) IN ((?, ?), (?, ?)). That works on SQLite (3.15+), MySQL and Postgres. The fk columns are selected beside the child's own, aliased (__elyra_key0, …), and decoded asP::Keyto group by.get_keyedbecomes the one-column case of this. - Eager,
belongs_to: the children's fk values, from their fields, then the parents with the sameIN. Keyed byp.key(). - Field relations look up by
self.key()(has_*) or the fk fields (belongs_to), so aVec<Member>field onTeamhydrates.
What doesn't change
For an i64 key every generated signature stays the same: load_* returns
HashMap<i64, _>, and the SQL for one column is today's. belongs_to_many
keeps its i64 requirement, now with a compile error that names the key
type when the parent's or the related model's key isn't i64.
Testing
model_servers.rs on SQLite always, and on MySQL and Postgres in CI: a
String-keyed parent and a composite parent, each through lazy and eager
has_many, has_one, belongs_to, the field forms, a child whose fk is
NULL, and a batch of parents with no children. Plus a test that the SQL
for an i64 relation is unchanged. compile_fail doctests: an fk tuple of
the wrong length, a belongs_to fk column the model doesn't have, a
composite parent without fk.
Implementation plan
elyra-db:Model::key,KeyFromRowand its impls, a multi-columnINclause,get_keyedoverK: KeyFromRow.eager_has_many,eager_has_oneandeager_belongs_tobecome generic over the key.elyra-macros: thefktuple, the checks,key(), the accessors and loaders over the key, and thebelongs_to_manyerror naming the key.- Tests and docs (
models.md: relations and composite keys), roadmap, CHANGELOG.
Open questions
Answered in Decisions.
fk = ("a", "b")orfk = "a, b"? A tuple reads as "several", and a string list is one more thing to split and validate. Recommendation: the tuple.fk = "a"stays as it is.load_*keyed byModel::Key, or newload_*_by_keymethods beside thei64ones? For ani64key the two are the same type. Recommendation:Model::Key. Nothing changes fori64, and other key types don't compile today, so nothing breaks.Model::keyas a required trait method, generated by the derive? A hand-writtenimpl Modelwould have to add it. Recommendation: yes, and say so under Upgrading. The trait's docs already describe it as generated, andKeycame in 0.51.- Very large batches: the
INlist isn't chunked today (SQLite allows 32766 bound values, Postgres 65535). A row-valueINuses two or more per parent. Recommendation: chunk the eager queries at 1000 parents. That's the same for one column and many, and below every driver's limit.
Not in this RFC
belongs_to_manywith non-i64keys: the pivot's columns, itsattachandsync, and RFC 0046's pivot rows all holdi64.- Polymorphic relations (
morphTo,morphMany). has_many_through.make:resourcefor a non-i64or composite model.
Alternatives considered
- Keep
i64maps and add a parallel API for other keys: two names for one thing. Every caller with ani64key is unaffected either way. - Group eager children in Rust by reading their fields instead of
selecting the fk columns aliased: the parent's derive can't see the
child's fields, so it would need a generated by-name getter for every
type. The alias costs one extra column per key column in the
SELECT. ORofANDs instead of a row-valueIN: portable to any SQL, but longer, and slower to plan on large batches. All three drivers support row values.
As built
As designed, with these specifics:
- A nullable foreign key was broken before, not only a non-
i64one.belongs_toread the fk withget_i64, which isNonefor anOption<i64>field, so a relation over a nullable fk never found its owner. The accessor now reads the fk fields themselves: aNULLin any part isOk(None), and the child is left out ofload_*. - An
fkof the wrong width onbelongs_tois caught byFromForeignKey, a hidden trait implemented only for a foreign key as wide as the owner's key: "abelongs_toforeign key(i64,)can't name an owner keyed by(i64, i64)". Aconstassertion onKEY.len()would have come after the type error it was meant to replace. belongs_to_many's related side is checked with a boundModel<Key = i64>spanned at the related type: "type mismatch resolving<Team as Model>::Key == i64".Query::where_in_rowsis public. With one column it renders the plainIN, and a unit test holds the SQL equal towhere_in's on all three drivers.- The old helpers stay, deprecated:
eager_has_many,eager_has_oneandeager_belongs_tokeep theiri64signatures for code that calls them; the derive useseager_many,eager_oneandeager_owners. - Tests:
framework/tests/model_servers.rsruns one walk-through on SQLite always and on MySQL 8 and Postgres 16 in CI (all three run locally too). It covers aString-keyed parent withhas_many,has_oneandbelongs_to, lazy and eager; a nullablei64fk; a composite parent both ways, with aNULLin one part of the fk; the field forms; and 1500 parents loaded through the chunks.elyra::relations_on_any_keyhascompile_faildoctests for a compositehas_manywithoutfk, anfkof the wrong width both ways, abelongs_tofk that isn't a column, andbelongs_to_manyto aString-keyed model.
Decisions
fk = ("a", "b"), a tuple of strings, for a key of more than one column;fk = "a"stays as it is.load_*maps are keyed byModel::Key: unchanged fori64, and other key types didn't compile before.Model::key(&self)is a required trait method, generated by the derive; Upgrading says a hand-writtenimpl Modeladds it.- The eager queries are chunked at 1000 parents, for one key column and for many alike.