Upsert relationships

async AsyncCogniteClient.relationships.upsert(
item: Relationship | RelationshipWrite | Sequence[Relationship | RelationshipWrite],
mode: Literal['patch', 'replace'] = 'patch',
) → Relationship | RelationshipList

Upsert relationships, i.e., update if it exists, and create if it does not exist.

Deprecated since version 8.23.0: The Relationships API is deprecated and is scheduled for removal on 2027-12-31. Model connections as edges or direct relations in the data modeling service. See https://docs.cognite.com/cdf/deprecated for details and migration guidance.

Note this is a convenience method that handles the upserting for you by first calling update on all items, and if any of them fail because they do not exist, it will create them instead.

For more details, see Upsert.

Parameters:
  • item (Relationship | RelationshipWrite | Sequence[Relationship | RelationshipWrite]) – Relationship or list of relationships to upsert.

  • mode (Literal['patch', 'replace']) – Whether to patch or replace in the case the relationships are existing. If you set ‘patch’, the call will only update fields with non-null values (default). Setting ‘replace’ will unset any fields that are not specified.

Returns:

The upserted relationship(s).

Return type:

Relationship | RelationshipList

Examples

Upsert for relationships:

>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes import RelationshipWrite
>>> client = CogniteClient()
>>> # async_client = AsyncCogniteClient()  # another option
>>> existing_relationship = client.relationships.retrieve(external_id="foo")
>>> existing_relationship.description = "New description"
>>> new_relationship = RelationshipWrite(
...     external_id="new_relationship",
...     source_external_id="new_source",
...     source_type="asset",
...     target_external_id="new_target",
...     target_type="event",
... )
>>> res = client.relationships.upsert(
...     [existing_relationship, new_relationship], mode="replace"
... )