Search code examples
neo4jcyphergraph-databasesneo4j-apoc

APOC trigger get nodes for removed relationship


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'})

Solution

  • 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'})