# Relationships

> How tables point at each other — ref, scope and fanout on a field, and the relationship kinds a recipe can declare.

Section: Recipes · Type: concept · Last updated: 2026-09-13

A relationship is a field that points at a real row in another table. ref names the target as Table.column; scope keeps parent and child agreeing on a shared column; fanout decides whether children spread evenly across parents or a few parents own most. The engine builds parents first, so every reference resolves. The relationship kinds a recipe can declare are after, aggregate, fk, polymorphic, unique.

## The picture

Parent (built first) → ref (Table.column) → Child (points at a real row)

scope keeps both rows agreeing on a column; fanout shapes how many children each parent gets.

## On a field

- ref (e.g. Customer.id) — Points at a real row in another table. Written as Table.column. The engine builds parents before children, so the value is always real.
- scope (e.g. state) — Only points at a row that agrees with this one. Names a column both rows share — the parent picked will match this row on it.

## On a table

- tree — A hierarchy built level by level — loops impossible.
- graph — Parts connected through an edge table, quantities multiplied.

## Relationship kinds

- after, aggregate, fk, polymorphic, unique. fk is honoured by the reference resolver rather than a kind branch. Values in the GraphRelationship doc comment but NOT listed here (sum, difference, product, ratio, consistency, derived) have no branch in generate.rs — use derive on the field instead.

## fanout values

- even, exact, flat, long_tail, one_to_one, power_law, steep, uniform

## A scoped reference with a hot-spot

```json
{
  "name": "customer_id",
  "ref": "Customer.id",
  "scope": "state",
  "fanout": "power_law"
}
```

The parent picked shares this row’s state, and a few customers get most of the rows.

## Common questions

**Can a child point at a parent in a different state?**

Not when scope names the column. scope: "state" makes the engine pick a parent whose state matches the child’s, so an address and its customer agree. Without scope any parent row is fair game.

**How do I make a few parents own most children?**

Set fanout on the ref field: power_law or steep for a hot-spot, even or flat for a fair spread, one_to_one or exact when each parent gets a fixed number. The choices are even, exact, flat, long_tail, one_to_one, power_law, steep, uniform.

## Related

- [Entities](https://theworklens.com/docs/recipes/entities): How a recipe declares its tables — name, row count, fields, and the table-level constructs that shape whole rows.
- [Rules & constraints](https://theworklens.com/docs/recipes/rules): What must hold on every row — ranges, uniqueness, date order, conditional presence, and constraints across columns.
- [Derived fields](https://theworklens.com/docs/recipes/derived-fields): Columns worked out from other columns — same-row arithmetic, totals from a child table, and roll-ups through a hierarchy.

_← Previous: [Fields & types](https://theworklens.com/docs/recipes/fields) · Next: [Rules & constraints](https://theworklens.com/docs/recipes/rules) →_

_Keys from the studio’s reference tab; kinds and fanout values from the engine vocabulary. Source: recipe-rules.ts + recipe-vocab.json._

_Canonical: https://theworklens.com/docs/recipes/relationships · This copy: https://theworklens.com/docs/recipes/relationships.md_
