Deletion from a trigger fails to cascade to more triggers
Issue by kraney
Tuesday Jul 16, 2019 at 23:50 GMT
Originally opened as https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/1258
Expected Behavior (Mandatory)
A trigger for deletedRelationship is called regardless of where or why the relationship was deleted.
Actual Behavior (Mandatory)
Trigger is not called when the relationship is deleted from inside another trigger.
How to Reproduce the Problem
Set up a two-hop path where each hop should be automatically deleted by a trigger. Only the first hop will be deleted because the second hop's trigger will never be called.
Simple Dataset (where it's possibile)
CREATE (a:Resource {name: 'foo'});
CREATE (s:Selector {name: 'sel'});
CREATE (k:KVP {key: 'foo', value: 'bar'});
CALL apoc.trigger.add('deleteOrphanedLabels',
"UNWIND {deletedRelationships} AS rel MATCH ()-[rel]->(l:KVP) CALL apoc.log.info('Called delete on %s', [l {.key, .value}]) DETACH DELETE l RETURN null",
{ phase: "before" });
CALL apoc.trigger.add('deleteOrphanedSelectors',
"UNWIND {deletedRelationships} AS rel MATCH ()-[rel]->(s:Selector) DETACH DELETE s RETURN null",
{ phase: "before" });
Steps (Mandatory)
1.MATCH (a:Resource {name: 'foo'}) DETACH DELETE a;
Specifications (Mandatory)
Currently used versions
Versions
- OS: Linux (inside docker on a mac)
- Neo4j: 3.5
- Neo4j-Apoc: apoc-3.5.0.4-all.jar
Comment by klongmitre
Thursday Jan 13, 2022 at 18:35 GMT
I'm seeing a similar problem where a SET on a node made inside a before trigger is not triggering another trigger that should be called when the field is changed. The second trigger does fire when I manually update the field. Have you been able to figure this out at all?
Hi there, there are a few syntax issues with your queries, but assuming you meant to create relationships etc in your sample dataset, I observed also the following:
From my understanding a "before" tag means the trigger is executed before the transaction is committed, but it is done so in the same transaction, so the relationship has already been deleted and can't be found within the query, same as if you did this yourself in one query:
A graph with:
(:FOO)-[:REL]->(:BAR)-[:REL]->(:BAZ)
MATCH (a:FOO)-[r:REL]->()
DETACH DELETE a
WITH r
MATCH ()-[r]->(b) // r no longer exists, so this finds nothing!
DETACH DELETE b
RETURN b.id
Only FOO is deleted.
I suspect your other issues with SET are also surrounding referencing/matching on deleted elements in the same transaction, which is why it would work manually.
I will close this as there isn't anything we can do about that behaviour, as it is how it is designed, APOC is building on transaction handling listeners and are not the same as triggers that are built in to other languages. In order to fix this we Neo4j would need to provide built in support for triggers :)