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.

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.

Thanks, making it as BEFORE seems to work now! :slight_smile: