cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Expect disruption to service on Thursday, Feb. 9!

How to use triggers to block update?

l_galrinho
Node Link

Hello, I'm using apoc 3.5.0.9 to implement triggers on my database, and I can't seem to find a way to have a trigger that blocks an update.

For instance, I have 10+ triggers that execute in "phase: after" once a certain property X is updated, and I wanted to have one other different trigger in "phase: before" that once said property X is to be updated, verified if it was a valid update based on a certain condition, and if not, it would be aborted and all the other 10+ triggers would not be activated.

What I'm trying to do is something similar to what is depicted below:

CALL apoc.trigger.add('blockUpdate','UNWIND apoc.trigger.propertiesByKey({assignedNodeProperties},"_executed") as prop
	WITH prop.node as n
	MATCH (n)<-[:someRelation]-(x) 
	WHERE  x._included=true OR x._executed=0
        --> ABORT AND CANCEL THE UPDATE ON NODE "n"
',
{phase:'before'});

Is this possible?

1 ACCEPTED SOLUTION

you can use call apoc.util.validate(<predicate>, <message>), seehttps://neo4j.com/docs/labs/apoc/4.0/misc/utility-functions/

View solution in original post

2 REPLIES 2

you can use call apoc.util.validate(<predicate>, <message>), seehttps://neo4j.com/docs/labs/apoc/4.0/misc/utility-functions/

l_galrinho
Node Link

Hi @stefan.armbruster, and thanks for the quick reply.

I've tried to use that function but I can't seem to get it right. I have noticed that it requires 3 arguments, the third one being a vector of arguments, and as such, I've used an empty one. This is what I have:

CALL apoc.trigger.add('blockUpdate','UNWIND apoc.trigger.propertiesByKey($assignedNodeProperties,"_executed") as prop
	WITH prop.node as n
        CALL apoc.util.validate( EXISTS((n)<-[:CONDITION]-(x{_included:true, _executed:0})), "Error msg", [])',
{phase:'before'});

But after this trigger is inserted into the database, I can't do anything else... Can't create nodes, delete or update them. This is the error message I get after trying to do any of these operations:

2X_5_5cdefba20d2389653b606c2ec557c12a62c34af7.png

Am I missing something concerning the syntax of this function?

Thanks again for your time.

EDIT (FIXED): If you just run that trigger neo4j will accept it, but if you have something in a query other than the CALL function you need to return something, and there is no error message concerning that. Instead, neo4j just crashes every other action, which is a rather odd behavior. To fix it, I just returned something after the CALL (it doesn't matter what is going to be returned). Just leaving this here in case it helps someone.

Once more, thank you.