Entity-to-Entity Relations
Entities can relate to to other entities.
Lynk manages all entity relationships centrally in one file entities_relationships.yml , to ensure consistency and simplicity. You can add entity relations via code, directly to this YAML file, or via the Studio UI.
Entity-to-entity relation
In this example, the entity nation relates to the entity customer in a one-to-many relationship (a nation has many customers)
# entities_relationships.yml
relationships:
nation-customer:
relationship: one_to_many
joins:
- name: nation_id
default: true
type: fields
fields:
- source: nation_id
destination: nation_id
operator: equalRelationships
RelationshipsThis is where we define all of our entity-to-entity relationships in the entities_relationships.yml file
Define a relationship by adding an object entity1-entity2.
In our example nation-customer, the direction is nation -> to -> customer.
Note that the direction applies to the Relationship parameter as well as to the source and destination parameters (see below)
Relationship
RelationshipSets the Relationship type between the two entities, according to the direction. In the above example, the entity nation has a one-to-many relationship with the customer entity (A nation can have many customers, and a customer can have only one nation).
The options for the Relationship property are:
one_to_oneone_to_manymany_to_manymany_to_one
Joins
JoinsDefine how the entities should be joined, by connecting features of the first (source) entity to features of the second (destination) entity.
name [optional]
name [optional]Give the join path a name.
If you choose not to name a join path, lynk will automatically name as the join path as "default" followed by a number. For example, the first unnamed join path will be named by Lynk to default_1, the second to default_2 etc..
Note that Join path name has to be unique on a related entity level.
default [optional]
default [optional]The default property takes boolean values (true / false).
There can be only one default join path between two entities. Lynk will use the default join path, unless a name of another join path will be stated.
In case there is more than one join path between an entity and a data asset, setting the default property becomes mandatory.
type
typeThe type property tells Lynk which method to use for defining the join path.
The options for type are:
sqlfieldslookup
sql (type)
sql (type)Use sql code to define how the two entities are related.
When setting type: sql, you will need to add the sql property with the SQL definition of the relation.
See the following example:
# entities_relationships.yml
relationships:
nation-customer:
relationship: one_to_many
joins:
- name: nation_id
default: true
type: sql
sql: "{source}.{nation_id} = {destination}.{nation_id}"Using the sql type;
{source} refers to the first entity
{destination} refers to the second entity
In the above example, the relationship direction is nation-customer. So the source is nation and the destination is customer.
fields (type)
fields (type)A simple YAML-style to define how two entities are related.
When setting type: fields, you will need to add the fields property with the fields and the operator that define the relation.
See the following example:
# entities_relationships.yml
relationships:
nation-customer:
relationship: one_to_many
joins:
- name: nation_id
default: true
type: fields
fields:
- source: nation_id
- destination: nation_id
- operator: equalUsing the fields type;
{source} refers to the first entity
{destination} refers to the second entity
operator defines the operator of the relation between the two fields.
The options for the operator property are:
equal
gt
gte
lt
lte
In the above example, the relationship direction is nation-customer. So the source is nation and the destination is customer.
The entity nation has a feature nation_id which connects to the feature nation_id of the entity customer, using the operator equal.
This translates to nation.nation_id = customer.nation_id
lookup (type)
lookup (type)Sometimes the relation between two entities is not direct. Meaning, in order to connect two entities we have to join them via one or more lookup tables ("hops"). Lynk supports this scenario using the lookup join type.
See the below example:
# entities_relationships.yml
relationships:
customer-organization:
relationship: many_to_one
joins:
- name: lookup_via_team
default: true
type: lookup
lookup:
- destination: db_prod.core.team
type: sql
sql: "{source}.{team_id} = {destination}.{team_id}"
- destination: organization
type: sql
sql: "{source}.{organization_id} = {destination}.{id}"In the above example, we connect the entity customer to the entity organization. Note that there is no direct connection between the two, and we have to join customer to db_prod.core.team first and then join db_prod.core.team to organization.
Using the lookup type;
As seen on the above code, lookups are an ordered list of steps;
On the first step,
sourceis always the first entityOn the last step,
destinationis always the second entityThe middle lookup assets should be data assets (not entities)
Inside each lookup step, we can define the connection between the source and destination, and choose how - using the type parameter as usual - SQL or fields
Last updated