Search code examples
neo4jneo4j-apoc

Apoc procedure - Trigger ID and increment


which is the correct APoc procedure to Trigger an ID and Increment it +1 every time I call a create?

Assuming User has an ID - my idea is to increment using ID and not UUID.

CALL apoc.trigger.add('create-event-gen-id',"UNWIND {createdNodes} AS e
MATCH (n:User) 
set e.ID=e.ID + 1", {phase:'after'});

Do you think this one is correct?

thank you.


Solution

  • This was how I solved My trigger.

    CALL apoc.trigger.add('triggeredID',
    "UNWIND $createdNodes AS e MATCH(n:User)  
    with e, MAX(n.ID) as maxId 
     Set e.ID = maxId + 1", {phase:'before'})
    

    please note that you have to make a Match after in order to get back the ID.