apoc.create.relationship

Details

Syntax

apoc.create.relationship(from, relType, props, to) :: (rel)

Description

Creates a RELATIONSHIP with the given dynamic relationship type.

Input arguments

Name

Type

Description

from

NODE

The node from which the outgoing relationship will start.

relType

STRING

The type to assign to the new relationship.

props

MAP

The properties to assign to the new relationship.

to

NODE

The node to which the incoming relationship will be connected.

Return arguments

Name

Type

Description

rel

RELATIONSHIP

The created relationship.

Usage Examples

The examples in this section are based on the following graph:

CREATE (p:Person {name: "Tom Hanks"})
CREATE (m:Movie {title:"You've Got Mail"});

This procedure provides a more flexible way of creating relationships than Cypher’s CREATE clause.

The example below shows equivalent ways of creating a node with the Person and Actor labels, with a name property of "Tom Hanks":

apoc.create.relationship
MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
CALL apoc.create.relationship(p, "ACTED_IN", {roles:['Joe Fox']}, m)
YIELD rel
RETURN rel;
CREATE clause
MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
CREATE (p)-[rel:ACTED_IN {roles:['Joe Fox']}]->(m)
RETURN rel;
Results
rel

[:ACTED_IN {roles: ["Joe Fox"]}]

But this procedure is mostly useful for creating relationships that have a dynamic relationship type or dynamic properties. For example, we might want to create a relationship with a relationship type or properties passed in as parameters.

The following creates relationshipType and properties parameters:

:param relType =>  ("ACTED_IN");
:param properties => ({roles: ["Joe Fox"]});

The following creates a relationship with a relationship type and properties based on the previously defined parameters:

MATCH (p:Person {name: "Tom Hanks"})
MATCH (m:Movie {title:"You've Got Mail"})
CALL apoc.create.relationship(p, $relType, $properties, m)
YIELD rel
RETURN rel;
Results
rel

[:ACTED_IN {roles: ["Joe Fox"]}]