Apoc Trigger in "Phase: After" Never Ends
Issue by leandrogalrinho
Thursday Mar 19, 2020 at 16:10 GMT
Originally opened as https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/1453
Hello, I've ran into an unexpected behavior and can't figure out what's the source of it. I'm using apoc 3.5.0.9, and I'm trying to have a trigger in "phase: after" that creates a relationship with the node that has been updated with some other node, but this causes the trigger to never end. However, if the trigger is in "phase: before" it all goes as expected.
Expected Behavior
The trigger is supposed to catch the update event and, after the transaction that updates the _executed property commits, it creates a new relationship with the updated node and a new node.
Actual Behavior
However, unless I terminate the query this never seems to end and will leave the graph in the same state it was before the update, except that the _executed property was indeed updated... But neither the node or the new relation is created.
How to Reproduce the Problem
Simple Dataset
CREATE (:TEST {name:'x', _executed:0});
CREATE (:TEST {name:'y', _executed:0});
CALL apoc.trigger.add('triggerTest','UNWIND apoc.trigger.propertiesByKey({assignedNodeProperties},"_executed") as prop
WITH prop.node as n
CREATE (z:SON {father:id(n)})
CREATE (n)-[:GENERATED]->(z)',
{phase:'after'});
Steps
- Insert dataset and trigger into neo4j.
- Perform this simple query that updates the _executed property of node x, activating the trigger:
MATCH (a:TEST)
WHERE a.name = 'x'
SET a._executed = a._executed+1
Specifications (Mandatory)
Currently used versions
Versions
- OS: Windows 8.1
- Neo4j: 3.5.14 Enterprise
- Neo4j-Apoc: 3.5.0.9
Comment by hikemachado
Tuesday May 05, 2020 at 18:35 GMT
I`m facing similar issue. I have a trigger for create relationship, in this trigger I need to create another relationship (but different relationship type) for the node. every time it is triggered for the specific relationshiptype it never ends
trigger definition:
CALL apoc.trigger.add("Cluster_TRG",
'UNWIND [rel in {createdRelationships} WHERE type(rel) = "IN_CLUSTER"] AS r with r, startNode(r) as nn, endNode(r) as cn match (cn)<-[]-(b) where id(b) <> id(nn) with nn, b match (b)-[outR:Depends_On]->(p) with nn, b, outR, p create (nn)-[nr:Depends_On]->(p) with p call apoc.log.info("Triggert called " + p.Name) return null',
{phase:'after'})
Versions
- OS : Windows 10
- Neo4j : 3.5.5 Community
- Neo4j-Apoc: 3.5.0-11
Comment by saibhaskerraju
Monday Jun 22, 2020 at 13:36 GMT
Hi All, Sincere apologies if this doesn't make sense I created the below trigger
CALL apoc.trigger.add('increase_followings_and_followers',
'UNWIND $createdRelationships AS rel
WITH rel, STARTNODE(rel) as follower, ENDNODE(rel) AS followed WITH rel, follower, followed
WHERE TYPE(rel)="HAS_E"
SET follower.followings = 1, followed.followers= 2',
{phase:'before'})
and this is the basic query I am writing
match(p:Post{id:1}) create(c:Comment{id:354}) merge(c)-[:HAS_E]->(p)
I have two scenarios CASE 1: The Phase in trigger = ' after' I see new :comment node getting created and attached to :Post node but the query doesn't stop. it keeps on looping CASE 2: Phase in trigger = ' before ' I don't see new :Comment node not relationship to :Post but query doesn't stop. it keeps on looping
I am running version 4.0.6 and apoc 4.0.0.15
Can someone please let me know what I am missing here.
Comment by magaton
Monday Jun 07, 2021 at 13:20 GMT
I have exactly the same problem. Neo4j 4.2.4, apoc, 4.2.0.4 When I run cypher within trigger, all is good, the same applies when trigger is set with phase:'before', but it never completes with phase: 'after'.
Comment by vga91
Wednesday Jun 30, 2021 at 07:34 GMT
@leandrogalrinho
With the latest Apoc version there is a phase: "afterAsync" to prevent some Transaction errors like this.
Please, could you try with this?
Hey! I have tested this out, as mentioned in the comment above, this works if you use the afterAsync phase. There is slightly new syntax if using apoc core these days, but this worked for me:
CALL apoc.trigger.install('neo4j', 'triggerTest', "UNWIND $assignedNodeProperties as prop WITH prop._executed as n, prop._executed[0].node AS x CREATE (z:TEST {prop: id(x)}) CREATE (x)-[:GENERATED]->(z)",
{phase:'afterAsync'});