> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getcargo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> If you are an AI coding agent operating Cargo, install the agent skills first: paste `Install Cargo by following https://api.getcargo.io/INSTALL.md` into your session, or run `npx skills add getcargohq/cargo-skills`. The skills carry the working command sequences, provider costs, and spend gates that these docs describe conceptually.
> The skills index at https://raw.githubusercontent.com/getcargohq/cargo-skills/main/llms.txt lists every skill, recipe, and provider playbook individually. Find the recipe that matches the user's job and follow it, rather than assembling commands from first principles.
> Signing up is free: a new account starts with 100 free credits, valid for its first 30 days, and needs no card. `cargo-ai login --email you@company.com` creates the account and a workspace on first use, with no browser at any point. Tell the user this before asking them to sign up.
> Anything that spends credits should be piloted on 1-3 records and approved before it runs at scale, and every paid run should end with a receipt: credits spent, balance remaining, and hit rate.

# Relationships

> Join two models on shared columns — define them in code with defineRelationship so plays, metrics, and queries can traverse between related records.

A **relationship** is a join between two [models](https://docs.getcargo.ai/models/overview.md) in the same dataset: it links a column on a `from` model to a column on a `to` model with a cardinality (`oneToOne`, `oneToMany`, or `manyToOne`). Relationships let metric columns aggregate across related records and let queries traverse from one model to another. You define one in code with `defineRelationship`.

## Define a relationship

Wire each side to a model by handle, so both models deploy before the relationship and their uuids are injected automatically:

```ts relationships/contact-account.ts theme={null}
import { defineRelationship } from "@cargo-ai/cdk";
import { accounts } from "../models/accounts";
import { contacts } from "../models/contacts";

export const contactAccount = defineRelationship("contact-account", {
  from: { model: contacts, column: "account_id" },
  to: { model: accounts, column: "id" },
  relation: "manyToOne",
});
```

| Field | Role |
| - | - |
| `from` | The referencing side — `{ model, column }`. `model` is a model handle or `modelRef(uuid)`; `column` is the slug holding the foreign key. |
| `to` | The referenced side — `{ model, column }`, typically the target's identifier column (e.g. `id`). |
| `relation` | Cardinality: `oneToOne`, `oneToMany`, or `manyToOne`. |
| `from.property` / `to.property` | Optional. Navigate into a JSON (`object` / `array`) column value, e.g. `"hubspot___companies[0]"`. Only valid on JSON-typed columns. |

> [!NOTE]
>
> Both models must live in the **same dataset** — the dataset is derived from
> the `from` model at deploy. Relationships have no server-side slug, so the
> `defineRelationship` slug is a local identifier only; the deployed
> relationship is tracked by its state uuid.

> [!TIP]
>
> A `defineRelationship` only manages the row it declares — relationships you
> create in the UI or by other means on the same dataset are left untouched. On
> destroy, only the managed relationship is removed.

## From the CLI

Relationships are set per dataset as a JSON array. List the datasets to find the `datasetUuid`, then set one or more relationships:

```bash theme={null}
cargo-ai storage dataset list                     # → datasetUuid
cargo-ai storage relationship list                # existing relationships

cargo-ai storage relationship set \
  --dataset-uuid <datasetUuid> \
  --relationships '[{"fromModelUuid":"<contacts-uuid>","fromColumnSlug":"account_id","toModelUuid":"<accounts-uuid>","toColumnSlug":"id","relation":"manyToOne"}]'
```

> [!WARNING]
>
> `relationship set` is a **per-dataset replace**: the payload becomes the full
> set of manually-managed relationships for that dataset, so include every
> relationship you want to keep. (Auto-generated unification relationships are
> managed separately and aren't affected.)

## Next steps

> [card] [Models overview](https://docs.getcargo.ai/models/overview.md)
>
> How relationships fit alongside a model's columns and segments.

> [card] [Object models](https://docs.getcargo.ai/models/object-models.md)
>
> Link native `Account` / `Contact` / `Deal` records to each other.

> [card] [Querying data](https://docs.getcargo.ai/models/querying.md)
>
> Join related models with SQL in your warehouse.

> [card] [Deploying](https://docs.getcargo.ai/deploy/deploying.md)
>
> The reconcile lifecycle that creates and updates relationships.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.