# Link

## Getting Started

Relationships between classes are defined by creating links in the class designer.

Starting in the **Class Designer,** click on the down arrow on the left side of the class you wish to work on. Next, expand the **Links** for the class. Click on **Please select** to choose the target class for the link. Click on the plus icon on the right of the textbox to create your link.

![Class designer: Creating a link](/files/-MBGA2-l_I0tUARHksaE)

There are different types of links.

**One-To-One**\
Class A can only have one instance of Class B and vice-versa.

**One-To-Many**\
Class A has several instances of Class B, but Class B can only be related to a single instance of Class A.

**Many-To-Many**\
Class A has several instances of Class B and Class B is related to several instances of Class A as well.

## Common Attributes

The Common section contains general properties of a link. In this section, you can specify how the link is presented in your application by setting the name, description and other information.

![Common attributes for links](/files/-MBFkOUqgdyWFv6IbKZV)

### Key

{% hint style="danger" %}
Currently disabled as this feature is still in development
{% endhint %}

### Parent Link

The parent link is used to specify the inheritance of abstract classes.

### Name

The link name is a user-friendly name that is normally generated automatically and set to the target class by default. This value usually does not require modifications.

{% hint style="info" %}
Changing the value for the **name** causes the link will be called the same in both classes.\
The **Name** field is compulsory when creating a link.
{% endhint %}

### Description

The description is a short help text that could potentially be used as hover text for the link.

### Link Setup

In the Link setup, you can configure the type and specifics of the link between the two classes.

{% hint style="success" %}
By checking **Use Link Service** in this section, several aspects of your link is handled by the platform and allows you to have a simpler configuration.

The following options are disabled when Link Service is being used:

* NM Class in **Common > Link Setup**
* Controller in **Advanced > Advanced**
* Type in **Advanced > Advanced**
  {% endhint %}

Normally, **Class 1** defines the origin class, while **Class 2** defines the target class.

**Field 1** identifies the field from **Class 1** that should be used as a distinct identifier (foreign key) and normally should be set to the primary key of that class. Similarly, **Field 2** specifies the distinct identifier (foreign key) for **Class 2** and should be set to the primary key of that class.

#### Is Source

The source setting defines the direction of a link. By selecting Is Source for a class, you can allow a user to navigate from that class to the other one.

{% hint style="info" %}
By selecting **Is Source** for both classes, the user is allowed to navigate in both directions.
{% endhint %}

#### NM Class

When configuring Many-To-Many link types, an extra intermediary table is required to cater for the complex relationship between both classes. The NM Class allows you to specify which table to use from the database. This table will be used to lookup the relations between the linked classes.

{% hint style="info" %}
For more information about many-to-many data models, click on [this link](https://en.wikipedia.org/wiki/Many-to-many_\(data_model\)).
{% endhint %}

## Advanced Attributes

In the advanced section of a link, you can further customize the how the link is processed by the platform by linking it to additional code and configuring additional options.

<figure><img src="/files/toclhSAxbM08z7Rg3CVB" alt=""><figcaption><p>Advanced attributes for links</p></figcaption></figure>

### Controller

The controller specifies additional code pertaining to the link that is running in the background.

{% hint style="danger" %}
This setting normally comes preconfigured as **Default**, which caters for most use cases and should only be modified to accommodate unique scenarios.
{% endhint %}

### Constraint

Link constraints refers to the restriction of links.

{% hint style="success" %}
For example, Constraints will allow you to define a link as a **one-to-many** link type, despite having the **NM Class** - which defines it as a **many-to-many** link type.

This can be relevant if the target class does not contain a field that can be used to define a normal **one-to-many** link.
{% endhint %}

### Direction

The link direction can be used to differentiate between two links.&#x20;

{% hint style="success" %}
For instance, if a class needs to be linked to itself, you can use **Direction** to define a parent and child in the link.
{% endhint %}

### Type

The class designer takes care of setting up most properties of your link. However, in this property, you can explicitly define a link type as **one-to-many** or **many-to-many**.

However, using the **NM Class** in the **Common Attributes** of a link instead can greatly simplify the configuration when creating your classes.

#### OneToMany

In the link type “OneToMany” a real relationship between one to many instances can be configured.

<figure><img src="/files/sz05dZtlTRKdyT1v5g9b" alt=""><figcaption></figcaption></figure>

| Field   | Required | Remarks                                                                       |
| ------- | -------- | ----------------------------------------------------------------------------- |
| Type    | Yes      | The type of link. Must be “OneToMany”.                                        |
| Class 1 | Yes      | The One class in One-To-Many. This is usually a main class.                   |
| Class 2 | Yes      | The Many class in One-To-Many. This is usually a dependent class.             |
| Field 1 | Yes      | The identifying field of class 1 in class 1. This is usually the primary key. |
| Field 2 | Yes      | The identifying field of class1 in class 2. This is usually a foreign key.    |

#### ManyToMany

In the link type “ManyToMany” a real relationship between many to many instances over a third class can be configured.

<figure><img src="/files/KXyvhzN5QxJaiAguVe8b" alt=""><figcaption></figcaption></figure>

| Field      | Required | Remarks                                                                                                                                                                                                                                                  |
| ---------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Type       | Yes      | The type of link. Must be “ManyToMany”.                                                                                                                                                                                                                  |
| Direction  | Yes      | <p></p><p>The direction of the relation:</p><ul><li>None: No specific direction is given.</li><li>Children: The direction is from class 1 (left) to class 2 (right).</li><li>Parents: The direction is from class 2 (right) to class 1 (left).</li></ul> |
| Class 1    | Yes      | The left hand Many class in Many-To-Many. This is usually a main class.                                                                                                                                                                                  |
| Field 1    | Yes      | The identifying field of class 1 in class 1. This is usually the primary key.                                                                                                                                                                            |
| NM Class   | Yes      | The relating class in Many-To-Many. This is usually a class only used for the relationship.                                                                                                                                                              |
| NM Field 1 | Yes      | The identifying field of class 1 in nm class. This is usually a foreign key.                                                                                                                                                                             |
| NM Field 2 | Yes      | The identifying field of class 2 in nm class. This is usually a foreign key.                                                                                                                                                                             |
| Class 2    | Yes      | The right hand Many class in Many-To-Many. This is usually a main class.                                                                                                                                                                                 |
| Field 2    | Yes      | The identifying field of class 2 in class 2. This is usually a primary key.                                                                                                                                                                              |

#### Service

In the link type “ManyToMany” a virtual relationship between many to many instances over a integrated service can be configured.

<figure><img src="/files/zu9JydnHqXbHX87xVGsK" alt=""><figcaption></figcaption></figure>

| Field   | Required | Remarks                                                                       |
| ------- | -------- | ----------------------------------------------------------------------------- |
| Type    | Yes      | The type of link. Must be “Service”.                                          |
| Class 1 | Yes      | The left hand Many class in Many-To-Many. This is usually a main class.       |
| Field 1 | Yes      | The identifying field of class 1 in class 1. This is usually the primary key. |
| Class 2 | Yes      | The right hand Many class in Many-To-Many. This is usually a main class.      |
| Field 2 | Yes      | The identifying field of class 2 in class 2. This is usually a primary key.   |

### Delete Behavior

Class-Designer option to not delete linked instances when deleting an instance. Following delete behavior options are available:

* Delete (default): Deletes all linked instances.
* Retain: Does not delete linked instances.

## Conditions

{% hint style="danger" %}
This feature is still in development at this time.
{% endhint %}


---

# 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.tivity.one/building-an-application/data/data-modelling/link.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.
