cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Freezed query after implemented AFTER trigger

After I added after trigger some of my subsequent queries just freeze and lock me, so I can not do any other CREATE. MATCH to see existing nodes just works fine and are successfuly created. I use Neo4j in version 3.5.16 enterprise

My trigger:

CALL apoc.trigger.add(
	'location_node_after',
	'UNWIND {createdNodes} AS node WITH node WHERE node:Location MATCH (target :Location) WHERE target.pk <> node.pk CREATE (node)-[r: PATH_TO { distance: distance(point({ latitude: target.lat, longitude: target.lon }), point({ latitude: node.lat, longitude: node.lon })) }]->(target)',
	{ phase: 'after' }
);

Query to try out:

CREATE
(random :Location { pk: 1, name: 'random', lat: 50.082010, lon: 14.418595 }),
(random2 :Location { pk: 2, name: 'random2', lat: 49.742635, lon: 14.717182 }),
(media1 :Media { pk: 1, taken_at: 1 }),
(person1 :Person { pk: 1, gender: 'female' })
WITH random, media1, person1
CREATE (person1)-[:TOOK_PHOTO]->(media1)-[:PHOTO_IN]->(random); // this will freeze

But this query works (it will create desired relation):

CREATE (:Location { pk: 1, name: "a", lat: 50.061512, lon: 14.604215});
CREATE (:Location { pk: 2, name: "b", lat: 50.063950, lon: 14.604086});

After I try to see running queries via apoc there is nothing active. When I try {assignedLabels} instead of createdNodes it behaves the same.

Thanks for helping me.

1 ACCEPTED SOLUTION

The after tigger code es executed in a separate transaction and it's itself subject to the after trigger, which is executed in a separate transaction, .....
You should actually see the new created nodes in the before phase.

In graph changes by triggers are normally done in before phase. after is used for post actions like writing to a log file or pushing some information about the change to another system.

View solution in original post

6 REPLIES 6

Have you tried to use phase:before instead? If you're using after it's done in a separate transaction that is subject to triggers as well, opening a recursive box of pandora.

I need to use AFTER trigger, becuase I need to work with newly created nodes. What's wrong with AFTER triggers? I just don't get it why it doesn't work - inside trigger I did not create any new nodes, thus no recursion should happen, shouldn't it?

The after tigger code es executed in a separate transaction and it's itself subject to the after trigger, which is executed in a separate transaction, .....
You should actually see the new created nodes in the before phase.

In graph changes by triggers are normally done in before phase. after is used for post actions like writing to a log file or pushing some information about the change to another system.

Hi @stefan_armbrust 
is it the problem with create triggers only or others as well?
I had tried to run the below trigger which is basically a trigger for update event and I had the same problem.
the trigger:

CALL apoc.trigger.add('updateLabela',"UNWIND apoc.trigger.nodesByLabel($removedLabels,'Actor') AS node
MATCH (n:Actor)
REMOVE n:Actor SET n:Person SET node:Person", {phase:'after'})

When I used the phase as 'before' it worked fine but incase of phase as 'after' the queries never got terminated. 
what could be the problem here?

In which case 'after' phase can be used and in which case it can't be?

 

Thanks, making it as BEFORE seems to work now!