---
title: Relationships
slug: v4-4/sYl9cRetVk5AmaWx_Iav0
createdAt: 2023-10-05T13:25:28.790Z
---

You can model relationships between your data using [foreign‑key relationships](https://docs-legacy.ditto.live/v4-4/V5eBeM0ww-xU1B4tE8Czc#N9oXG) and [key‑value pair relationships](https://docs-legacy.ditto.live/v4-4/V5eBeM0ww-xU1B4tE8Czc#F9rQb) by way of embedded `maps` and `arrays`. For more information about Ditto's advanced types, see [Data Types](https://docs-legacy.ditto.live/v4-4/tAWkWoXz6TuRtoc-1VFVI).

:::hint{type="warning"}
Avoid using `arrays` in Ditto.&#x20;

Due to potential merge conflicts when offline peers reconnect to the mesh and attempt to sync their updates, especially when multiple peers make concurrent updates to the same item within the `array`.
:::

:::hint{type="info"}
Ditto does *not* support nesting documents within documents. If you need to create relationships between documents, use a foreign-key relationship by referencing the document ID. For more information, see [Foreign-Key Relationships](./#foreign-key-relationship), as follows.
:::

:::hint{type="info"}
For an example demonstrating both the deeply embedded and flat models, see [Complex Structures](https://docs-legacy.ditto.live/v4-4/7oLGf7NkMBlkR8ehnpczC).
:::

# Overview

The following table provides a complete overview of the different relationships you can form in Ditto, as well as a brief description, list of possible approaches you can take, and links to related content:

| **Relationship** | **Description**                                                                                                                                                      | **Approaches**                                                                                                      |
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| One-to-many      | Associates a parent element with children elements to establish a hierarchy.                                                                                         | - Embed a `map`<br />- Reference a field to a document<br />- Reference a document to a collection                  |
| Many-to-many     | Associates multiple entities in one collection with multiple entities in another collection.                                                                         | - Embed a `map`<br />- List items in an `array`<br />- Create references between documents in different collections |
| Many-to-one      | Associates two or more collections, where one collection refers to the primary key of another collection to create a meaningful relationship between the datasets.   | - Embed a `map`<br />- Create references between documents in different collections                                 |

# Foreign-Key Relationship

You can represent a one-to-many or many-to-one relationship between two or more collections, in which one collection refers to the primary key of another collection to create a meaningful relationship between the datasets. &#x20;

For example, the following snippet demonstrates a foreign-key relationship between documents in the `cars` and `people` collections, in which the reference to `susanId` serves as the foreign key establishing a relationship between `cars` and `people`:

:::CodeblockTabs
pseudocode

```javascript
const results = await ditto.store.write(async (transaction) => {
   // Create a person named Susan in the "people" collection
  const cars = transaction.scoped('cars')
  const people = transaction.scoped('people')

  // Create a car document in the "cars" collection
  const susanId = await people.upsert({
    name: 'Susan',
  })
  await cars.upsert({
    make: 'Hyundai',
    color: 'red',
    owner: susanId, // Set the owner field to Susan's ID
  })
  
  // Evict the Susan document from the "people" collection
  await people.findByID(susanId).evict()
})
```
:::

# Key-Value Relationship

A *key-value relationship&#x20;*&#x65;stablishes a parent-child hierarchy between embedded data elements where the key serves as the parent and the associated values for the children to link related data items as well as organize them in a structure that enhances the efficiency of lookup.

When managing data that requires unique identifiers and relationships, instead of using an `array` to encode your data, use a `MAP` with unique string keys and object values instead.&#x20;



