# Many-to-many relationships

A many-to-many relationship describes the case where multiple records in one [object class](https://docs.appfarm.io/reference/data-model/object-classes) are related to multiple objects in another object class.&#x20;

* A Person has many Skills, and each Skill has many Persons.
* A Project has many Project members (Persons), and each Person is a member of many Projects.

Appfarm supports multi-references, i.e. [Object Class Properties](https://docs.appfarm.io/reference/data-model/object-class-properties) with Cardinality Many, to hold a list of references towards an Object Class or Enumerated Types. This is a great option if you *only* need information about references. For example: `Person.Skills` may be a multi-reference towards the `Skill` object class, if you only need to know the list of skills for a person (and which persons that have a certain skill) - and you do not need to know e.g. which level a person has.

Note that you may also define multi-reference properties as [runtime properties](https://docs.appfarm.io/reference/apps/data/data-sources#runtime-properties) on data sources, or [App variables](https://docs.appfarm.io/reference/apps/data/app-variables#properties).

{% hint style="info" %}
Multi-references (Object Class Properties with cardinality Many) are set up in the Global Data Model and may be used in the UI (Tables, Multi Select, List ++) and Logic (Foreach, Conditions ++) directly. This guide highlights the key features, but you may find these articles useful as well:&#x20;

* [Object Class Properties](https://docs.appfarm.io/reference/data-model/object-class-properties#general-properties)
* [Multi Select](https://docs.appfarm.io/library/ui-components/multi-select#selection-type)
* [Conditions](https://docs.appfarm.io/reference/platform-concepts/conditions#example-5)
* [Update Object and Value Processor](https://docs.appfarm.io/library/action-nodes/update-object#multi-cardinaliy-value-processor)
  {% endhint %}

This guide describes how to create many-to-many relationships in Appfarm Create.

## Example of a many-to-many relationship (using multi-reference)

Say you have an object class `Person` to store all your organization's employees and an object class `Skill` to store all the skills relevant to your organization. You would like to be able to track which employees have which skills (but not which level).

Since a `Person` can have multiple `Skills`, and the `Skills` are considered to be information related to the `Person` (and not Persons being information related to Skills), we need the following:

* A `Person` object class
* A `Skill` object class
* A property `Person.Skills`, with a reference towards the `Skill` object class, with Cardinality `Many`.&#x20;

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2Ftbkq60OSlx5X280W5FyO%2Fimage.png?alt=media&#x26;token=78f24055-dcba-46f1-ba69-4f162b2ff09e" alt=""><figcaption></figcaption></figure>

### Multi-reference: Key features

`Person.Skills` may contain a list of `Skill ID`s, and in general you may treat this property as a Data Source with Cardinality Many. Here are some key features:

* `Person.Skills` can be set in a [Create Object](https://docs.appfarm.io/library/action-nodes/create-object) or an [Update Object](https://docs.appfarm.io/library/action-nodes/update-object#multi-cardinaliy-value-processor) action node, or directly in the UI using a [Multi Select](https://docs.appfarm.io/library/ui-components/multi-select) (with selection type *Property*)
* You may add or remove IDs to/from `Person.Skills` using Update Object and a value processor, as in [**this example**](https://docs.appfarm.io/library/action-nodes/update-object#multi-cardinaliy-value-processor).&#x20;
* You may iterate `Person.Skills` in a [Foreach](https://docs.appfarm.io/library/action-nodes/foreach) Action Node or an [Iterating Container](https://docs.appfarm.io/library/ui-components/container#custom-lists) or [Table](https://docs.appfarm.io/library/ui-components/table) in your UI.
* You may use Filter and Conditions towards Person.Skills as if it was a multi-cardinality data source. \
  \&#xNAN;*Note that the wording is slightly different (Has Some Of / Has None Of instead of Exists In / Not Exists In). You may read more on this example in the* [*Conditions article*](https://docs.appfarm.io/reference/platform-concepts/conditions#example-5)*.*\
  ![](https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2F1Np0cguWmdwDRYcCEme2%2Fimage.png?alt=media\&token=01871c5c-b1c9-47d2-9db1-39f1b813934d)

However, if you need to store information about *what level each Person has for each Skill*, you may not use the above approach. See the following example.

## Example of a many-to-many relationship (using a many-to-many Object Class)

Given the above example: We now want to save information about what Skill Level each Person has for each Skill.&#x20;

In that case, we need the following:

* A `Person` object class
* A `Skill` object class
* An enumerated type `Skill level`
* A `Person Skills` object class

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2F4eoEUvqP1vrztjCmh93C%2Fimage.png?alt=media&#x26;token=d657ee51-086d-4c82-a794-723e7f02c704" alt=""><figcaption></figcaption></figure>

`Person Skills` is a many-to-many relationship object class that contains references to the two object classes to be related: Person and Skills. In addition, it has a reference to the enum Skill level. For each Person's skill, a record should be created into this Object Class (holding the ID of the Person and Skill, and the Value of the Skull level enum).

To create the `Person Skills` object class:

#### Step 1

Add a [new object class](https://docs.appfarm.io/reference/data-model/object-classes#usage) to your data model. The best practice is to name this object class using a combination of the names of the two object classes to be related, for example `Person Skill`.

#### Step 2

Add a [new property](https://docs.appfarm.io/reference/data-model/object-class-properties) to the newly created object class. This property will store references to another object class that represents one half of the relationship.

* **Property Name**: Best practice is to use the name of the object class, for example `Person`.
* **Data Type**: Select the object class to relate under **References**, in this case `Person`.
* **Cardinality**: One.

#### Step 3

Add another new property to the object class. This property will store references to another object class that represents the other half of the relationship.

* **Property Name**: Best practice is to use the name of the object class, for example `Skill`.
* **Data Type**: Select the object class to relate under **References**, in this case `Skill`.
* **Cardinality**: One.

#### Step 4

Add another new property to the enumerated type Skill level.&#x20;

* **Property Name**: Best practice is to use the name of the object class, for example, `Skill level` or just `Level`
* **Data Type**: Select the enumerated type to relate under **Enums**, in this case `Skill level`.
* **Cardinality**: One.

Now you have an object class that references two other object classes. Each record added to the object class represents one connection between the two object classes. For example, an individual person and one skill that they possess, and the level. Each employee will have a unique record for each skill they possess and each skill will be represented based on how many employees possess that skill.

Note that to use these object classes in an app or service you need to add [data sources](https://docs.appfarm.io/reference/apps/data/data-sources).


---

# Agent Instructions: 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.appfarm.io/how-to/data-modeling/many-to-many-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.
