Skip to main content
Version: Next

Relationship Types

A RelationshipType defines a typed, directional connection between entities. Because relationships are themselves entities (of type entity_type/relationship_type), they can carry their own properties (edge properties) and participate in further relationships.

Defining a RelationshipType​

Properties​

FieldTypeRequiredDescription
namestringyesSystem identifier; used as the key
labellocalized mapyesDisplay name per locale -- the noun at the target end (Ingredient, not Has Ingredient)
label_plurallocalized mapnoPlural of label; defaults to label
label_inverselocalized mapnoThe label read from the target's side -- the noun at the source end (Recipe). Leave it empty where the relationship has no sensible inverse reading
label_inverse_plurallocalized mapnoPlural of label_inverse; defaults to label_inverse
descriptionlocalized mapnoDescription per locale
iconstringnoIcon identifier for UI display
target_typeentity referenceyesConstrains the entity type of the target
key_scriptLua stringnoComputes keys for relationship instances. See Lua Scripting.
label_scriptLua stringnoComputes labels for relationship instances. See Lua Scripting.

Why Four Labels?​

A relationship is shown as a tab on both entities it connects, and the two tabs hold different things: the source's tab lists targets (label / label_plural), the target's tab lists sources (label_inverse / label_inverse_plural). Each label is therefore the noun of what the tab contains, singular and plural -- never a sentence about the relationship.

Relationships on a RelationshipType​

RelationshipTarget TypeDescription
has_propproperty_typeEdge properties carried by each relationship instance
has_relrelationship_typeFurther relationships that relationship instances can participate in

Because a relationship instance is an entity, a relationship type may also declare lists (has_list), components and traits; a relationship payload then carries them beside its edge properties.

Example​

{
"key": "has_ingredient",
"properties": {
"name": "has_ingredient",
"label": { "en_us": "Ingredient", "de_de": "Zutat" },
"label_plural": { "en_us": "Ingredients", "de_de": "Zutaten" },
"label_inverse": { "en_us": "Recipe", "de_de": "Rezept" },
"label_inverse_plural": { "en_us": "Recipes", "de_de": "Rezepte" },
"target_type": "entity_type/vegetable"
},
"relationships": {
"has_prop": [
{
"properties": { "name": "quantity", "label": { "en_us": "Quantity" }, "required": true },
"target": "int_property_type/generic"
},
{
"properties": { "name": "unit", "label": { "en_us": "Unit" } },
"target": "string_property_type/generic"
}
]
}
}

This defines a has_ingredient relationship type that targets vegetable entities and carries quantity and unit as edge properties.

Relationship Instances​

A relationship instance represents a concrete connection between two entities. It is itself a full entity with its own UUID, properties, and metadata.

Fields​

FieldDescription
sourceroot_id of the source entity that owns this relationship -- not a commit-specific id. To compare a relationship's source with an entity, compare it with that entity's root_id.
targetReference to the entity being pointed to (id, root_id, or type_name/key form)
propertiesEdge property values defined by the relationship type's has_prop

The relationship belongs to the source entity as a whole, not to one of its commits, so it stays valid when the source gets new commits. Which relationships an entity currently has is determined by the manifest of its current commit -- see Ownership and Manifests. Editing an edge property creates a new commit of the relationship and of its source entity.

Creating Relationship Instances​

Relationships can be created inline when creating or updating an entity:

{
"key": "lasagna",
"properties": { "name": { "en_us": "Lasagna" } },
"relationships": {
"has_ingredient": [
{
"target": "vegetable/tomato",
"properties": { "quantity": 3, "unit": "pieces" }
},
{
"target": "vegetable/onion",
"properties": { "quantity": 1, "unit": "pieces" }
}
]
}
}

They can also be created through the dedicated relationship routes:

POST /api/v1/by-key/recipe/lasagna/relationships/has_ingredient

See REST API Reference for the full relationship route documentation.

Relationship Keys and Sub Key Spaces​

Relationship instances have their own key, computed by the key_script on the RelationshipType (following the same resolution precedence as entity keys).

Key and unique edge-property uniqueness is scoped to a sub key space for the combination of (relationship_type, source_entity). Two relationship instances of the same type may share a key or a unique edge-property value as long as they have different source entities.

Canonical Reference Format​

The canonical reference for a relationship instance follows the form:

source_entity_type/source_entity_key/relationship_type/relationship_key

For example: recipe/lasagna/has_ingredient/tomato

Relationship instances also carry their own root_id and id, with the same versioning semantics as regular entities.

Declaring Relationships on Entity Types​

Entity types declare which relationship types they participate in via has_rel:

"has_rel": [
{
"properties": { "name": "has_ingredient", "label": { "en_us": "Ingredients" } },
"target": "relationship_type/has_ingredient"
}
]

The target points to the RelationshipType entity. Only relationship types declared via has_rel can be used on instances of that entity type.

Built-in System Relationship Types​

These are defined by the system and used to describe the meta model itself:

NameTargetDescription
has_propproperty_typeDeclares a property on an entity type or relationship type
has_virtual_propproperty_typeDeclares a computed property
has_relrelationship_typeDeclares a relationship on an entity type or relationship type
has_listentity_typeDeclares a list on an entity type, relationship type or trait
has_componententity_typeDeclares an owned, embedded typed sub-entity (component)
has_parententity_typeInheritance link
has_lc_maplc_mapLinks an entity type to a lifecycle map
has_versioning_schemeversioning_schemeBinds a versioning scheme to an entity type or instance
has_lc_statuslc_statusLinks a lifecycle map to its statuses
has_lc_transitionlc_statusDefines a valid transition from one lc_status to another
has_actionactionDeclares an action on an entity type
has_hookactionDeclares a hook on an entity type
info

System relationship types are part of the meta model and cannot be modified by customers. They form the foundation on which all data modeling is built.

Traversing Relationships via the API​

The API provides several ways to navigate relationships:

RouteDescription
GET /@outgoingAll outgoing relationships, keyed by relationship type UUID
GET /@incomingAll relationship entities whose target points to this entity (only relationships endorsed by the current commit of each source)
GET /@referenced-byAll entities that reference this entity through an entity-reference property (e.g. file_ref, enum_ref) -- not through relationships; use @incoming for those
GET /relationshipsAll relationships grouped by relationship type name
GET /relationships/<rel_name>All instances of a specific relationship type
GET /relationships/<rel_name>/<rel_root_id>/@targetNavigate through a relationship to the target entity

See REST API Reference for the complete traversal documentation.