Swift Dynamic Fields
Dynamic Fields
Dynamic fields reshape JOIN-heavy rows into nested generated models. Add
@@{ ... } annotations near the SELECT or view columns that should form a
nested value.
SELECT
t.id AS task__id,
t.title AS task__title,
n.id AS note__id,
n.body AS note__body
/* @@{ dynamicField=notes,
mappingType=collection,
sourceTable=n,
aliasPrefix=note__ } */
FROM task t
LEFT JOIN task_note n ON n.task_id = t.id;
The important pieces are:
dynamicFieldnames the generated property.mappingTypechoosesperRow,collection, orentity.sourceTablepoints to the table or alias that owns the nested fields.aliasPrefixtells SQLiteNow which selected columns belong to the nested value.
Generated Swift SELECT models expose nested properties directly:
let tasks = try await db.task.selectWithNotes().list()
let firstNote = tasks.first?.notes.first
Use stable aliases for complex views. That keeps generated names predictable and prevents unrelated joined columns from leaking into the top-level result.