# Tables Relationships

## Content

DataManager supports defining relationships between tables.
A relationship connects a field in one table to a field in another table.
After a relationship is created, fields from the related table can be accessed using the relationship name.
Relationships are defined at the `DataManager` level.

## Creating a Relationship

Use [`addRelationship`](/spreadjs/api/classes/GC.Data.DataManager#addrelationship) to define a relationship between two tables.

```javascript
dataManager.addRelationship(
  sourceTable,
  sourceFieldName,
  sourceRelationshipName,
  targetTable,
  targetFieldName,
  targetRelationshipName
);
```

### Parameters

* **sourceTable** \- The table where the relationship is defined\.
* **sourceFieldName** \- The field in the source table used for matching\.
* **sourceRelationshipName** \- The name used to access the related table from the source table\.
* **targetTable** \- The related table\.
* **targetFieldName** \- The field in the target table used for matching\.
* **targetRelationshipName** \- The name used to access the source table from the target table\.

The method returns an `IRelationship` object describing the relationship.

## Example

```javascript
const dataManager = spread.dataManager();

const orders = dataManager.addTable("orders", {
  data: [
    { orderId: 1, orderDate: "2024-01-01", employeeId: 100 },
    { orderId: 2, orderDate: "2024-01-02", employeeId: 101 }
  ]
});

const employees = dataManager.addTable("employees", {
  data: [
    { employeeId: 100, firstName: "Nancy", title: "Manager", address: "Seattle" },
    { employeeId: 101, firstName: "Andrew", title: "Sales", address: "Tacoma" }
  ]
});

// Create relationship
const relationship = dataManager.addRelationship(
  orders,
  "employeeId",
  "employee",
  employees,
  "employeeId",
  "orders"
);

// Access related fields in a view
const myView = orders.addView("myView", [
  { value: "orderId" },
  { value: "orderDate" },
  { value: "employee.firstName" },
  { value: "employee.title" },
  { value: "employee.address" }
]);
```

In this example:

* `"employee"` is the relationship name defined on the `orders` table.
* Related table fields are accessed using dot notation:

```javascript
relationshipName.fieldName
```

## Accessing Related Data

After a relationship is created, related fields can be referenced in a View using:

```javascript
relationshipName.fieldName
```

Example:

```javascript
{ value: "employee.firstName" }
```

The relationship name acts as a navigation path to the related table.

## Relationship Storage

Relationships are stored in the `DataManager`.
Each call to `addRelationship` creates a new relationship entry.
Multiple relationships can coexist within the same `DataManager`.
Relationship names are defined by the developer. The API does not enforce uniqueness of relationship names.

## Removing a Relationship

Use `removeRelationship` to remove a relationship by name:

```javascript
dataManager.removeRelationship("employee");
```

After removal, related field access through that relationship name is no longer available.