A trigger automatically executes predefined operations in response to specific data events on nodes. Triggers can run before or after an insert operation, allowing you to enforce business rules, maintain data integrity, or perform auxiliary operations without manual intervention.
SyntaxCREATE TRIGGER "triggerName" ON NODE "labelName" [COMMENT 'description'] BEFORE|AFTER INSERT CALL " $entity call { ... } "
triggerName: The identifier for the trigger, must be unique within the graph.labelName: The node label the trigger is associated with. The label does not need to exist at creation time; the trigger will not fire until the label is created.COMMENT: An optional description of what the trigger does.BEFORE|AFTER INSERT: When the trigger fires relative to the insert operation.CALL: The callable body that defines the trigger logic.The trigger body uses the format $entity call { ... } and supports let assignments to modify entity properties. The let keyword must be lowercase.
Single Assignment$entity call { let entity.propertyName = expression }
Multiple assignments can be separated by semicolons:
Multiple Assignments$entity call { let entity.name = upper(entity.name); let entity.role = 'employee' }
| Expression Type | Example | Description |
|---|---|---|
| String literal | 'active', "active" | A quoted string value. |
| Numeric literal | 18, 3.14 | An integer or float value. |
| Property access | entity.name | Reads a property from the current entity. |
| Function call | upper(entity.name) | Applies a built-in function to an expression. |
Function | Description |
|---|---|
upper(value) | Converts a string to uppercase. |
lower(value) | Converts a string to lowercase. |
trim(value) | Removes leading and trailing whitespace from a string. |
len(value) / length(value) | Returns the length of a string. |
Create a trigger that converts the name property to uppercase before insertion:
GQLCREATE TRIGGER "AutoUpperCase" ON NODE "Student" COMMENT 'Converts name to uppercase before insertion' BEFORE INSERT CALL " $entity call { let entity.name = upper(entity.name) } "
Now inserting a Student node will automatically uppercase the name:
GQLINSERT (n:Student {name: "John Doe"}) RETURN n.name
| n.name |
|---|
| JOHN DOE |
Create a trigger that sets a default property value:
GQLCREATE TRIGGER "DefaultRole" ON NODE "Employee" COMMENT 'Sets default role to intern' BEFORE INSERT CALL " $entity call { let entity.role = 'intern' } "
Syntax-- Drop a trigger (error if not found) DROP TRIGGER "triggerName" -- Drop only if it exists (no error if not found) DROP TRIGGER IF EXISTS "triggerName"
NOTEDropping a node label will also remove any triggers associated with that label.
Syntax-- List all triggers in the current graph SHOW TRIGGERS -- Filter by node label SHOW TRIGGERS ON NODE "labelName" -- Filter by edge label SHOW TRIGGERS ON EDGE "labelName"
Returns a table with the following columns:
Field | Description |
|---|---|
name | The name of the trigger. |
entity_type | The entity type: NODE. |
label | The node label the trigger is associated with. |
event | The trigger event, such as BEFORE_INSERT or AFTER_INSERT. |
enabled | Whether the trigger is active. |
comment | The optional description of the trigger. |
BEFORE trigger fails, the insert is aborted.AFTER trigger fails, the error is logged but the insert is not rolled back.