- Relationship Types
- Patterns — lookup · master_detail · Junction Object · tree
- Delete Behaviors · Roll-up Summaries
- Inline Editing (Master-Detail Entry) · Detail-page related lists
- Incorrect vs Correct
| Type | Lifecycle | Required | Sharing | Roll-ups | Use Case |
|---|---|---|---|---|---|
lookup |
Independent | Optional by default | Independent | Via summary |
"Related to" |
master_detail |
Coupled (cascade delete) | Forced only under controlled_by_parent, unless the optional master (below); else lint-warned |
Inherits parent; read-on-master opt-ins (below) | Via summary |
"Owned by" |
tree |
Self-reference | Optional — and so is reference, which when written must name this object itself; any other object is refused at parse, not lint-warned |
N/A | Not available | Hierarchical |
export default ObjectSchema.create({
name: 'contact',
sharingModel: 'public_read_write',
fields: {
first_name: { type: 'text', required: true },
account_id: {
type: 'lookup',
reference: 'account',
},
}
});// Parent
export default ObjectSchema.create({
name: 'invoice',
sharingModel: 'public_read_write',
fields: {
invoice_number: { type: 'text', required: true },
}
});
// Child
export default ObjectSchema.create({
name: 'invoice_line_item',
sharingModel: 'controlled_by_parent',
fields: {
invoice_id: {
type: 'master_detail',
reference: 'invoice',
deleteBehavior: 'cascade',
required: true,
},
product: { type: 'text', required: true },
amount: { type: 'currency', required: true },
}
});A controlled_by_parent detail row naming a master inherits its access: reads are scoped
to accessible masters and, by default, every write needs edit on the master.
An opt-in on the detail asks read on it instead:
sharingModel: 'controlled_by_parent',
sharing: { controlledByParent: { writeRequiresMasterRead: true } }, // or ownerMayEditownerMayEdit: truewidens only the row owner's (owner_id) by-id update; every other write, on-behalf-of included, still needs master edit.writeRequiresMasterRead: true: whoever reads the master inserts, updates and deletes the detail under its own object permissions, RLS and FLS (re-points and each opted-in chain hop too; on-behalf-of, both users must read the master). A predicate (multi: true) update or delete writes the rows a by-id write of each would, all matched rows or none. The ownership floor (rows you created) still binds, for both users, on-behalf-of updates and deletes. Refused besideownerMayEdit: true, which it covers. Lintsecurity-controlled-by-parent-owner-policy-without-opt-inis silent here.
The detail's own row policies AND in under both. Each is refused at parse under any
other sharingModel and one level up (sharing.KEY).
"A rep sees the contacts of the accounts they own; a contact need not have an
account": the detail's only master_detail declared required: false, never
an owner field copied by hooks:
account: { type: 'master_detail', reference: 'account', required: false },A row with no master is its owner's (owner_id): the owner or View All on the
detail reads it, the owner or Modify All writes it (no owner: View All / Modify
All only). An insert naming no master is admitted. Setting a master asks edit
on it, emptying one edit on the stored master (both even under ownerMayEdit;
read for both under writeRequiresMasterRead). A
chain hop with no master ends there under the same rule. Refused beside a
second master_detail or as readonly/system; ObjectSchema.create() reads
an omitted required as true.
Import resolves a detail's name within its master when the row sets that
master (one reference column to it) or the lookup's dependsOn sibling: a tie
there is reference_ambiguous, another master's detail reference_out_of_scope
(scope: [{ field, value }] names the scoping columns). A name no record
has, or a scoping field the caller cannot read, is reference_not_found.
export default ObjectSchema.create({
name: 'project_assignment',
sharingModel: 'public_read_write',
fields: {
project_id: {
type: 'lookup',
reference: 'project',
required: true,
},
employee_id: {
type: 'lookup',
reference: 'employee',
required: true,
},
role: { type: 'text' },
hours_allocated: { type: 'number' },
},
indexes: [
{ fields: ['project_id', 'employee_id'], unique: 'organization' },
],
});export default ObjectSchema.create({
name: 'category',
sharingModel: 'public_read',
fields: {
name: { type: 'text', required: true },
parent_category: {
type: 'tree',
reference: 'category',
},
}
});Configure deleteBehavior on master_detail — cascade or restrict only;
an authored set_null is refused at publish (it would orphan the detail row).
| Behavior | Effect | Use Case |
|---|---|---|
cascade |
Delete all child records | Invoice → Line Items |
restrict |
Prevent parent deletion if children exist | Department → Employees |
The walk also reads lookup and tree (set_null by default — the field is
cleared; a multiple field loses only that member — or an authored cascade /
restrict), and a user field only when it declares a value: deleting the user
then cascades, clears or refuses exactly as a lookup does; undeclared, the
field keeps the deleted user's id. restrict, and set_null on a required
field (a multiple one only when the removal would empty it), refuse the delete
with 409 DELETE_RESTRICTED.
Change Owner: transferBehavior: 'cascade' (default 'none') on a child's lookup /
master_detail has the parent's Change Owner dialog offer to move the child rows its
previous owner owns too; declare it where children follow the parent's owner. Parse
refuses the key on other types, and cascade on a sys_* reference, multiple: true,
or a child not offering Change Owner itself (a controlled_by_parent detail).
A parent summary field aggregates a child collection. The engine recomputes it
server-side on every child insert/update/delete, in the write's
transaction.
{
"type": "summary",
"summaryOperations": { "object": "invoice_line", "field": "amount", "function": "sum" }
}object is the child object to aggregate, field its field (ignored for
count), function one of count / sum / min / max / avg. Optional:
filter (below) and relationshipField (omitted: the child's lookup /
master_detail referencing this parent). Refused at publish when omitted and the
child has neither, or naming no child field
(rollup/child-without-parent-reference).
Empty collections roll up to 0 for count/sum, null for min/max/avg.
Pick the function from the request's own words, and put every qualifier in
filter. "how many" / "number of" → count; "total" / "sum" → sum;
"average" → avg. A qualified roll-up — "completed task count", "open ticket
count", any "done / pending / overdue / STATE" count or sum — carries that
condition in filter (a query where FilterCondition, ANDed with the
parent-FK match). Dropped, the field counts EVERY child and shows a plausible
wrong total that no check on the parent can see — worse than a visible 0. This
is also how one child feeds several totals: completed / in-progress / overdue
task counts are three summary fields over the same child, differing only in
filter. A select's value in filter is its option value, never its label;
operator and compound forms work
(filter: { status: { $in: ['received', 'partial'] } }). A rate (occupancy,
completion rate = a child count ÷ a capacity) is a formula over a working
summary — author the summary first; the rate computes only once it does.
The recompute runs only on a child write. A summary declared after its
parent rows exist reads NULL on every existing parent until a child of it is
written. The backfill is os migrate summary-nulls --apply
(--recompute-undefined-on-empty OBJECT.FIELD for a min / max / avg
declared late) — never an edit to a child. Declare the summary with the object
where you can; declared late, run the migration before you call the number ready.
Never finish with security-rollup-source-unreadable standing. The total
covers every child row and answers only to the parent field's own access, so
os build warns for each permission set that reads the summary but not the
child object, the aggregated field, or a field filter tests. Decide per set
from the request: it sees the children → grant the read; the total but not the
children → keep the column readable and add rollupSourceUnreadableReason to
the set's entry for it (the warning becomes a note quoting the reason; refused
blank or beside readable: false); neither → mask the summary on that set.
Never add the reason to silence a total the request withholds: mask it.
A permission set (in TypeScript, the argument of definePermissionSet(...))
with no task read — warned; task_count masked — resolved; hours_total,
which clients are meant to see, acknowledged — a note:
{
"name": "client_viewer",
"objects": { "project": { "allowRead": true } },
"fields": {
"project.task_count": { "readable": false },
"project.hours_total": { "readable": true, "rollupSourceUnreadableReason": "Clients see progress totals, not tasks" }
}
}The parent's standard create/edit form enters a child's rows where a
field group of the parent names that child (relatedList): the group draws
the child's table at its place in the record's details and in the parent's
entry form (below the form's fields when its sections name no field group),
and parent + children save in one atomic transaction. A parent or child
declaring enable.apiMethods must list 'bulk', or that save is refused 405 OBJECT_API_METHOD_NOT_ALLOWED.
// PARENT `invoice`: the group places the lines and holds no fields
fieldGroups: [{ key: 'lines', label: 'Line Items', relatedList: 'invoice_line' }],
// CHILD `invoice_line`: its master_detail FK, declaring no `relatedList`
invoice: {
type: 'master_detail',
reference: 'invoice',
inlineTitle: 'Line Items', // optional grid title
// inlineColumns / inlineAmountField — optional overrides; columns are
// otherwise derived from this object's fields.
},Name only true line-item / composition children (invoice lines, order items, expense lines); leave associations (comments, attachments, activity, audit) as tabs.
The rendering side: objectstack-ui → Master-Detail Forms.
inlineEdit only picks how a placed child's rows are entered:
inlineEdit: 'grid' // editable line-item grid (fast bulk entry; thin children)
inlineEdit: 'form' // read-only list; "Add" / per-row edit opens the FULL formOmitted (a boolean is refused) — smart default: picks form when the child has
rich/form-only fields (textarea, file, image, json, location…) or more than ~8
editable fields, else grid.
Modeling a 'grid' line item for the full editor:
// Parent (a field group names invoice_line, as above)
Invoice = {
fields: {
tax_rate: Field.number({ label: 'Tax Rate (%)' }), // → live Subtotal/Tax/Total stack
total: Field.summary({ summaryOperations: {
object: 'invoice_line', field: 'amount', function: 'sum' } }),
},
}
// Child line
InvoiceLine = {
fields: {
invoice: Field.masterDetail('invoice', { inlineEdit: 'grid' }),
position: Field.number({ defaultValue: 0 }),
product: Field.lookup('product', { required: true }),
description: Field.text(),
quantity: Field.number({ required: true, defaultValue: 1 }),
unit_price: Field.currency(),
amount: Field.currency({ expression: 'record.quantity * record.unit_price' }),
},
}- Computed column — a stored
currency/numberfield with anexpressionrenders read-only and recomputes live in the grid, then persists. Keep it stored (NOT aformulafield) so the parentsummarycan roll it up; the engine computes it from the row the write stores, so a client-sent value never stands. It reads only that row: a related record's value (record.product.unit_price) keeps what it held at this row's last write (warningstored-expression-cross-record-input), so it stays aformula(read time, not totallable) or a snapshot a flow on the related object writes; repair a field left stored withos migrate stored-expressions --object OBJECT --field FIELD --changed --apply. The clock (now(),today(),daysFromNow(),daysAgo()), the acting user,can()andhasCapability()are fixed at the write (warningstored-expression-volatile-input): aformulakeeps them current (the clock: a scheduled flow too). - Catalog auto-fill — a
lookupline field + sibling columns whose names match fields on the referenced record (e.g.unit_price,description) → picking a record fills those cells (acurrencyamount only in the same currency: field-types.md →lookupAutofill). - Sort field — a numeric
position/sort_order/sequencefield is auto-detected, hidden from the grid, and stamped on drag-reorder.
On the parent's record detail page every child relationship is a related list,
shown by default as a tab with its count, a master_detail child too. The
relationship flags refine that:
project: {
type: 'master_detail',
reference: 'project',
relatedListTitle: 'Tasks',
relatedListColumns: ['title', 'status', 'priority', 'due_date'],
},
audit_ref: { type: 'master_detail', reference: 'invoice', relatedList: false },relatedList: false— the child object is not shown on the parent record page.'primary'— a full-width table in the record's details, with no tab. A parent field group naming amaster_detailchild (relatedList: 'quote_line', written only when asked) places its table there too; its tab stays unlessrelatedListsomits it.- The parent's
relatedListsis the tab list (objectstack-ui Record Page): declared, only the named lists show, in order, built-ins by name (sys_attachment,sys_approval_request,sys_activity). An entry is the child,object.fieldor{ list, label, columns }; itslabeltranslates atobjects.{parent}._relatedLists.{list}.label(none: no key). A.customarrangement re-stating the entry with nolabel, or hiding it, leaves none andos validaterefuses the key: drop it, or re-state the entry with the owner'slabel. A child in the stack with no inbound FK is refused everywhere; one that exists nowhere is refused byos validate/os build(bar a platform or sibling-artifact name) and by a metadata write naming no package (one into a package is stored, placing nothing).
relatedListTitle / relatedListColumns override the derived title / columns
(columns: the child's highlightFields); an entry's label / columns win. Audit FKs
(created_by/updated_by/owner_id) never become related lists. A child that
references the parent through several FKs (e.g. primary_account +
partner_account) yields one related list per FK; a self-referential FK
surfaces a "children" list.
A line item on a lookup can exist without its parent: use master_detail.
No many_to_many field type exists: use a junction object.