---
title: "Lusid.Relationship.Definition.Writer"
slug: "lusidrelationshipdefinitionwriter"
updated: 2024-08-21T12:32:15Z
published: 2024-08-21T12:32:15Z
canonical: "support.lusid.com/lusidrelationshipdefinitionwriter"
---

> ## Documentation Index
> Fetch the complete documentation index at: https://support.lusid.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lusid.Relationship.Definition.Writer

| **Type** | **Read/write** | **Author** | **Availability** |
| --- | --- | --- | --- |
| [Data provider](/v1/docs/what-are-a-data-provider-and-a-direct-provider) | Write | Finbourne | Provided with LUSID |

The `Lusid.Relationship.Definition.Writer` provider enables you to write a [Luminesce SQL query](/v1/docs/understanding-the-luminesce-sql-query-syntax) that either upserts or deletes [relationship types](/v1/docs/creating-relationships-between-certain-types-of-entity) between entity types that support relationships.

> **Note:** The LUSID user running the query must have sufficient [access control permissions](/v1/docs/creating-policies-to-control-access-to-providers-for-different-luminesce-end-users) to both use the provider *and* interact with relationship type data in LUSID. This should automatically be the case if you are the domain owner.

You must construct a valid table of data to write, one relationship type per record. `Lusid.Relationship.Definition.Writer` lists the fields (columns) available to populate with values for each record, and has a set of parameters to help you construct a valid table.

Your query can use the `WriteAction` field to perform one of the following operations:

- Upsert a relationship type; that is, create one if it does not exist, and update it if it does. This is the default operation if you omit `WriteAction`.
- Insert (create) a relationship type. This operation fails if the relationship type already exists.
- Update a relationship type. Note it is only possible to update the `DisplayName`, `OutwardDescription` and `InwardDescription` fields. This operation fails if the relationship type does not exist.
- Delete a relationship type.

**See also:** [Lusid.Relationship.Definition](/v1/docs/lusidrelationshipdefinition)

## Basic usage

```sql
@table_of_data = <select-statement>;
select * from Lusid.Relationship.Definition.Writer where toWrite = @table_of_data;
```

## Query parameters

`Lusid.Relationship.Definition.Writer` has parameters that help you construct a valid table of data to write.

> **Note:** The `toWrite` parameter is mandatory and used to actually write the table of data into LUSID.

To list available parameters, their data types, default values, and an explanation for each, run the following query using a [suitable tool](/v1/docs/what-tools-are-available-to-write-luminesce-queries):

```sql
select FieldName, DataType, ParamDefaultValue, Description from Sys.Field where TableName = 'Lusid.Relationship.Definition.Writer' and FieldType = 'Parameter';
```

## Data fields

`Lusid.Relationship.Definition.Writer` lists the fields you can populate in your table of data to write.

Depending on the operation you want to perform, the following fields are mandatory to include in the table of data to write:

| **Operation** | **Specify using...** | **Mandatory fields** | **Notes** |
| --- | --- | --- | --- |
| Upsert | `'Upsert' as WriteAction` (or omit) | `Scope` `Code` `DisplayName` `SourceEntityType` `TargetEntityType` `OutwardDescription` `InwardDescription` `Lifetime` `RelationshipCardinality` | `SourceEntityType` and `TargetEntityType` must be one of the [entity types that support relationships](/v1/docs/creating-relationships-between-certain-types-of-entity). They can be the same entity type. If either source or target is a [custom entity](/v1/docs/extending-lusids-data-model-using-custom-entities), specify the custom entity type name. `Lifetime` can either be `Perpetual` (the default) or `TimeVariant`. `RelationshipCardinality` can either be `ManyToMany` (the default) or `ManyToOne`. |
| Insert | `'Insert' as WriteAction` |
| Update | `'Update' as WriteAction` | `Scope` `Code` `SourceEntityType` `TargetEntityType` `Lifetime` `RelationshipCardinality` | You can update the `OutwardDescription`, `InwardDescription` and `DisplayName` fields. |
| Delete | `'Delete' as WriteAction` | `Scope` `Code` |  |

To list all available fields, their data types, whether fields are considered 'main', and an explanation for each, run the following query using a [suitable tool](/v1/docs/what-tools-are-available-to-write-luminesce-queries):

```sql
select FieldName, DataType, IsMain, IsPrimaryKey, SampleValues, Description from Sys.Field where TableName = 'Lusid.Relationship.Definition.Writer' and FieldType = 'Column';
```

## Write errors

We recommend examining the results of every write query using one or more of the `WriteError`, `WriteErrorCode` and `WriteErrorDetail` fields.

For each record in the table of data to write, `Lusid.Relationship.Definition.Writer` returns an error code. If the operation is successful, the error code is `0`. If unsuccessful, a positive error code and explanation help you discover why LUSID considers the operation invalid.

For example, the query:

```sql
@table_of_data = select 'PortfolioManagementTeam' as Scope, 'Managers' as Code, 'Portfolio' as SourceEntityType, 'Person' as TargetEntityType,
'TimeVariant' as Lifetime, 'ManyToOne' as RelationshipCardinality;
select WriteErrorCode, WriteError, WriteErrorDetail from Lusid.Relationship.Definition.Writer where toWrite = @table_of_data;
```

...fails because no `DisplayName`, `OutwardDescription` and `InwardDescription` were provided in the query.

## Examples

> **Note:** For more example Luminesce SQL queries, visit our [Github repo](https://github.com/finbourne/luminesce-examples/tree/master/examples).

### Example 1: Create a relationship type between portfolio entities and person entities

You can create a relationship type by supplying the mandatory fields and using `'Insert' as WriteAction`.

```sql
@table_of_data = select 'PortfolioManagementTeam' as Scope, 'Managers' as Code, 'Authorised managers for portfolios' as DisplayName, 
'Portfolio' as SourceEntityType, 'Person' as TargetEntityType, 'Is managed by' as OutwardDescription, 'Manages' as InwardDescription,
'TimeVariant' as Lifetime, 'ManyToOne' as RelationshipCardinality, 'insert' as WriteAction;
select * from Lusid.Relationship.Definition.Writer where ToWrite = @table_of_data;
```

### Example 2: Create a relationship type between custom entities

If either `SourceEntityType` or `TargetEntityType` are custom entities, specify the [custom entity type name](/v1/docs/extending-lusids-data-model-using-custom-entities#defining-a-custom-entity-type):

```sql
@table_of_data = select 'Locations' as Scope, 'Branches' as Code, 'Regional offices and local branches' as DisplayName, 
'Office' as SourceEntityType, 'Office' as TargetEntityType, 'Responsible for' as OutwardDescription, 'Reports to' as InwardDescription,
'TimeVariant' as Lifetime, 'ManyToOne' as RelationshipCardinality, 'insert' as WriteAction;
select * from Lusid.Relationship.Definition.Writer where ToWrite = @table_of_data;
```

### Example 3: Update a relationship type with additional details

You can update an existing relationship type, perhaps to change the `DisplayName`, by supplying the mandatory fields, the field(s) you want to update, and by using `'Update' as WriteAction`.

```sql
@table_of_data = select 'PortfolioManagementTeam' as Scope, 'Managers' as Code, 'These are the authorised managers for portfolios' as DisplayName,
'Portfolio' as SourceEntityType, 'Person' as TargetEntityType, 'Is managed by' as OutwardDescription, 'Manages' as InwardDescription,
'TimeVariant' as Lifetime, 'ManyToOne' as RelationshipCardinality, 'update' as WriteAction;
select * from Lusid.Relationship.Definition.Writer where ToWrite = @table_of_data;
```

### Example 4: Delete a relationship type

You can delete an existing relationship type by specifying a `Scope` and `Code` and by using `'Delete' as WriteAction`:

```sql
@table_of_data = select 'PortfolioManagementTeam' as Scope, 'Managers' as Code, 'delete' as WriteAction;
select * from Lusid.Relationship.Definition.Writer where ToWrite = @table_of_data;
```

### Example 5: Create multiple relationship types at the same time

You can create all your relationship types at the same time in one Luminesce query. In this example, the query defaults to an upsert operation as no explicit `WriteAction` is specified.

```sql
@vals = values
('PortfolioManagementTeam', 'Managers', 'Authorised managers for portfolios', 'Portfolio', 'Person', 'Is managed by', 'Manages', 'TimeVariant', 'ManyToOne'),
('Instruments', 'InstrumentLegalEntities', 'Issuers for instruments', 'Instrument', 'LegalEntity', 'Is issued by', 'Issues', 'Perpetual', 'ManyToOne');

@table_of_data = select column1 as Scope, column2 as Code, column3 as DisplayName, column4 as SourceEntityType, column5 as TargetEntityType, 
column6 as OutwardDescription, column7 as InwardDescription, column8 as Lifetime, column9 as RelationshipCardinality from @vals;

select * from Lusid.Relationship.Definition.Writer where ToWrite = @table_of_data;
```
