apoc.create.relationship

Procedure APOC Core

apoc.create.relationship(person1,'KNOWS',{key:value,…​}, person2) create relationship with dynamic rel-type

Signature

apoc.create.relationship(from :: NODE?, relType :: STRING?, props :: MAP?, to :: NODE?) :: (rel :: RELATIONSHIP?)

Input parameters

Name Type Default

from

NODE?

null

relType

STRING?

null

props

MAP?

null

to

NODE?

null

Output parameters

Name Type

rel

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;
Table 1. 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;
Table 2. Results
rel

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