I want to create an apoc trigger that will trigger when a relationship is removed. The trigger needs to find the nodes m
and n
that the deleted relationship was connecting. Below is what I have, but MATCH (m)-[deletedRel]->(n)
does not find any matches. What is the correct way to find the two nodes that were connected by a removed relationship?
CALL apoc.trigger.add('myTrigger',
"UNWIND $deletedRelationships as deletedRel
WITH apoc.trigger.toRelationship(deletedRel, $removedRelationshipProperties) AS deletedRel
WITH deletedRel WHERE apoc.rel.type(deletedRel) = 'MY_REL_TYPE'
MATCH (m)-[deletedRel]->(n)
WITH deletedRel, m, n
CREATE (r:Report {myId: id(deletedRel), type: apoc.rel.type(deletedRel)})" ,
{phase:'before'})
You can try using the startNode
and endNode
functions, like this:
CALL apoc.trigger.add('myTrigger',
"UNWIND $deletedRelationships as deletedRel
WITH apoc.trigger.toRelationship(deletedRel, $removedRelationshipProperties) AS deletedRel
WITH deletedRel WHERE apoc.rel.type(deletedRel) = 'MY_REL_TYPE'
WITH deletedRel, apoc.rel.startNode(deletedRel) AS m, apoc.rel.endNode(deletedRel) AS n
CREATE (r:Report {myId: id(deletedRel), type: apoc.rel.type(deletedRel)})" ,
{phase:'before'})