memgraphdbmemgraph

Memgraph trigger which only acts upon one relationship


How can I create a trigger which will automatically create a distance property on the USES relationship only? The following trigger throws an error during the CREATE statement.

create trigger setDistance 
ON --> CREATE BEFORE COMMIT EXECUTE
unwind createdEdges as createdEdge
WITH createdEdge
WHERE createdEdge.distance is null and createdEdge:USES
set createdEdge.distance = 0;

create (:Seller)-[:USES]->(:Country);
  ERROR: Trigger 'setDistance' caused the transaction to fail.
  Exception: Only nodes have labels.

Thanks!


Solution

  • Currently the variable:Thing syntax within a WHERE clause is just for node label filtering, see documentation. You can use the type function to get an edge's type as a string, see documentation.

    Hence with how memgraph is currently the trigger should be:

    CREATE TRIGGER setDistance 
    ON --> CREATE BEFORE COMMIT EXECUTE
    UNWIND createdEdges AS createdEdge
    WITH createdEdge
    WHERE createdEdge.distance IS NULL AND type(createdEdge) = "USES"
    SET createdEdge.distance = 0;