1
votes

I'm trying to use triggers on Neo4J to record in a CSV the details of a node or a relationship when it is deleted. After experimenting with different triggered queries I found that trying to write details of deleted relationships directly to a csv doesn't work, but creating a virtual node to record the relationship details and then writing the v-node to csv does. The query I used is this:

call apoc.trigger.add("deletingRels","UNWIND {deletedRelationships} as del WITH del call apoc.create.vNode(['Record'], {startUUID:startNode(del).uuid, endUUID:endNode(del).uuid, relType:type(del)}) YIELD node as r WITH r, collect(r) as rs,'D:/export/reldeltest.cql' as filename call apoc.when(size(rs) > 0,'call apoc.export.csv.data(rs,[],filename,{quotes:false,useTypes:false}) YIELD rows RETURN rows','',{rs:rs,filename:filename}) YIELD value RETURN value",{phase:"before"})

Running the query:

match (t:Test {name:"test 12"})-[r]->(s:Test {name:"test 7"}) delete r return t,s

deletes the relationship r and satisfyingly results in the following csv:

"_id","_labels","startUUID","relType","endUUID","_start","_end","_type"
"-4",":Record","f8ccb190-fd43-11e7-866b-40b034e34b1f","testR","ad45c410-fd24-11e7-90a4-40b034e34b1f",,,

The analogous trigger for a deleted node is:

call apoc.trigger.add("deletingNodes", "UNWIND {deletedNodes} as del WITH del call apoc.create.vNode(['Record'], {uuid:del.uuid, nodeLabels:labels(del)}) YIELD node as r WITH r, collect(r) as rs,'D:/export/nodedeltest.cql' as filename call apoc.when(size(rs) > 0,'call apoc.export.csv.data(rs,[],filename,{quotes:false,useTypes:false}) YIELD rows RETURN rows','',{rs:rs,filename:filename}) YIELD value RETURN value", {phase:"before"})

Running a query to delete a node results in the following error message in the Neo4J browser:

Neo.ClientError.Transaction.TransactionHookFailed

and the error log in the console starts:

WARN  Error executing trigger deletingNodes in phase before Node with id 1364 has been dele
ted in this transaction
org.neo4j.graphdb.QueryExecutionException: Node with id 1364 has been deleted in this transaction
        at org.neo4j.kernel.impl.query.QueryExecutionKernelException.asUserException(QueryExecutionKernelException.java:
35).

Running a query to match the node shows that it still exists.

I'm running a zipped version of Neo4J 3.3.1 on Windows, with the apoc 3.3.1 plugin installed, in a console.

Can anyone help please?

1

1 Answers

0
votes

I think we should make it possible to simplify the whole process of logging updated/deleted data.

Having said that, I think it's b/c you access the deleted node's properties del.uuid which are no longer available/accessible, I think you might be able to access id(del) and labels(del) b/c they are in the node-record.

For the relationship, you also only access type(del) but no properties.

The deleted properties should be available in a separate (earlier) removed-properties-trigger.