Links, Lookups and Rollups
By the end of this chapter two tables know about each other: every item names the place it is stored, every place can count its items and list what kinds they are — and none of those answers is typed in by hand. This is the chapter the whole tool exists for.
The problem links solve
The inventory knows eight items. Where is the chess set? In a spreadsheet the answer would be a ninth column of typed text — "Hall Closet", "hall closet", "Closet (hall)" — three spellings that sort apart and agree on nothing. A link field replaces the typing with a reference: the item points at a row of the Locations table. Misspellings become impossible (you pick from the rows that exist), renaming the closet renames it everywhere at once, and the Locations table can answer questions about its items — which is the part a typed column can never do.
Making the link
In the app: add a field, choose
Links, choose the other table, and
choose the shape of the relationship.
Shapes matter and the guide names them
plainly: one-to-many (one location
holds many items; each item sits in one
place — this lab's shape), many-to-many
(a book has many authors, an author has
many books), and one-to-one. The
lab's call (transcript t04) also caught
this build's API dialect: the relation's
properties belong at the top level of
the request in 0.301.5, and nesting
them the way older documentation shows
earns a precise complaint —
Missing 'parentId' property in request body.
What appears afterwards, on reading both tables back, is the part to remember:
- On Items, a link field StoredIn — and beneath it a real foreign key column in the database, which is the honest machinery doing the work.
- On Locations, a matching link field Items, created automatically. A relationship in NocoDB is always visible from both ends; you build it once, from either side.
Filling the link
Chapter 6's warning pays off here: a link is not set by the record insert. It has its own endpoint, per row (transcript t05):
curl -X POST .../tables/<items-id>/links/<link-field-id>/records/1 \
-d '[{"Id":1}]'
Eight calls put each item in its location, each answering 201. In the app the same act is choosing the location from a dropdown in the item's row — the dropdown is this endpoint with better manners.
Rollup: a count that keeps itself
A rollup reads across a link and
aggregates: count the linked rows, or
sum, average, take the earliest date,
and so on. The lab added ItemCount
to Locations — a rollup over the Items
link, function count — and the moment
the links landed, the Locations table
answered (transcript t05):
| Location | ItemCount |
|---|---|
| Garage Shelf | 3 |
| Kitchen Pantry | 2 |
| Hall Closet | 3 |
Move an item and these numbers move; no one maintains them. The rollup's other functions over a numeric field — sum of Quantity, say — are the stock-taker's version of the same trick.
Lookup: reading a value across
A lookup pulls a field from the linked rows and shows it where you stand. CategoriesHeld, on Locations, looks up Category across the Items link, and reports:
Garage Shelf [Tools, Tools, Tools]
Kitchen Pantry [Kitchen, Kitchen]
Hall Closet [Books, Games, Games]
The duplicates are left in on purpose
in this telling, because they teach the
type precisely: a lookup lists the
values it finds, one per linked row,
without tidying, counting or
deduplicating them. Want "how many" —
that is a rollup. Want "which ones,
roughly" — that is a lookup. Between
link, lookup and rollup, most of what
people beg spreadsheets to do with
VLOOKUP incantations becomes a field
you declare once and never maintain.
And the formula, briefly
The fourth computed type, Formula,
works inside one row: the lab's
TotalValue is Quantity * UnitValue,
written with field names in braces and
stored by the server translated to
column ids (transcript t04 shows the
stored form, with error: null —
formulas are checked when saved, not
when read). It recomputed itself in
chapter 6 when a quantity changed. The
formula language has the usual
arithmetic plus functions for text,
dates, conditions and logic; the
documentation lists them all, and this
guide's one worked example is honestly
enough to show the shape of the thing.