> For the complete documentation index, see [llms.txt](https://docs.lingoql.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lingoql.com/sub0/creating-database-models/table-relationships.md).

# Table Relationships

Use `foreign_key` to define relationships between tables or collections in a Sub0 database model.

### Defining a relationship

To create a relationship, add a `foreign_key` object to a field.

```json
{
  "user_id": {
    "type": "Number",
    "foreign_key": {
      "of": "_user",
      "key": "id"
    }
  }
}
```

This declares that:

* `user_id` references the `id` field on the `_user` table
* The relationship is enforced at the database level where supported

***

### `foreign_key` properties

| Property | Type     | Description                          |
| -------- | -------- | ------------------------------------ |
| `of`     | `String` | Target table/collection name         |
| `key`    | `String` | Referenced field on the target table |

***

### One-to-many relationships

This is the most common relationship pattern in Sub0.

**Example:** A user can have many transactions.

```json
{
  "user_id": {
    "type": "Number",
    "foreign_key": {
      "of": "_user",
      "key": "id"
    }
  }
}
```

* One `_user` record → many `_transaction` records
* Defined by placing the foreign key on the “many” side

***

### Many-to-one relationships

Implicitly handled by the same structure.

```json
{
  "order_id": {
    "type": "Number",
    "foreign_key": {
      "of": "_order",
      "key": "id"
    }
  }
}
```

***

### Required vs optional relationships

Relationships are **required by default**.

To allow a nullable relationship, mark the field as optional:

```json
{
  "user_id": {
    "type": "Number",
    "optional": true,
    "foreign_key": {
      "of": "_user",
      "key": "id"
    }
  }
}
```

***

### Relationships across databases

Sub0 applies relationship definitions according to each database engine:

* **PostgreSQL / MySQL / MariaDB**\
  Foreign key constraints are enforced at the database level.
* **MongoDB**\
  Relationship fields can still be defined in the model, but MongoDB does not enforce foreign key constraints natively.

***

### Nested objects vs relationships

Use relationships when:

* Data lives in separate tables/collections
* Records must be independently queryable
* Referential integrity matters

Use `JSON_OBJECT` when:

* Data is tightly coupled
* No independent lifecycle is required
* Joins are unnecessary

***

### Summary

* Relationships are declared using `foreign_key`
* Defined at the field level
* Database-agnostic and enforced appropriately
* Optional relationships are explicitly marked

Next, see [Indexing](/sub0/creating-database-models/indexing.md) for `indexable` and `fts` field options.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lingoql.com/sub0/creating-database-models/table-relationships.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
