python-3.xneo4jcypherneo4j-apocneo4j-driver

Returning the altered (by apoc.trigger) version of node instead of the original


I am using the python bolt driver to create nodes in a neo4j database. These nodes get altered by apoc.trigger functions. And i want the returning BoltStatementResult to contain the altered version of these nodes.

This is what i have tested so far:

  1. My triggers are working as expected. The nodes stored, are altered correctly.
  2. I tried the 'before' and 'after' phases.
  3. I set the trigger functions to return the altered version.
  4. I did write a second query to get the new and updated node of database. But this option is quite unsafe, as it has no unique identifier.

My trigger function:

CALL apoc.trigger.add(
    'onCreateNodeAddMetadata',
    'UNWIND {createdNodes} AS n
     SET n.uid = apoc.create.uuid(), n.timestamp = timestamp() RETURN n',
    {phase: 'before'}
)

I expect the returning value of my session.write_transaction to contain the the added properties.


Solution

  • As a safe workaround (but see caveat below), the Cypher query in your write_transaction can return the native ID of the created node (e.g., RETURN ID(n)).

    Then, as long as you know the node was not deleted, you can perform a query for it with that ID (in this example, myID contains the ID value and is passed as a parameter):

    MATCH (n) WHERE ID(n) = $myId
    ...
    

    If the node could be deleted before you search for it by native ID, then this technique is not safe, since neo4j can re-assign the native ID of a deleted node to another newly- created node.