Constraints

Constraints enforce entity-identity rules at the schema level. Declare them with the ISO-GQL FOR … REQUIRE … IS form:

CREATE CONSTRAINT person_email FOR (n:Person) REQUIRE n.email IS UNIQUE
CREATE CONSTRAINT person_id    FOR (n:Person) REQUIRE n.id    IS PRIMARY KEY
CREATE CONSTRAINT robot_serial FOR (r:Robot)  REQUIRE r.serial IS NOT NULL
FormEnforces
IS UNIQUENo two nodes of the label share the property value.
IS PRIMARY KEYUnique and present — the node's identity key.
IS NOT NULLThe property must be present on every node of the label.

A write that violates a constraint returns a typed error and rolls back.

Drop a constraint#

DROP CONSTRAINT person_email

List constraints#

CALL db.constraints()

Causal constraints#

REQUIRE <prop> CAUSED_BY (:<AnchorLabel>) declares that any write to the named property on a node of the given label must carry a same-transaction :CAUSED_BY edge to a node of the anchor label.

CREATE CONSTRAINT FOR (n:Player)
  REQUIRE confidence CAUSED_BY (:Evidence)

Writes that violate the constraint fail at commit with a typed CAUSED_BY_VIOLATION error and roll back the entire transaction. See Causal Edges for the full discipline.

To apply the rule going forward without rejecting data that predates it, add WITH GRANDFATHER_EXISTING:

CREATE CONSTRAINT FOR (n:Player)
  REQUIRE confidence CAUSED_BY (:Evidence) WITH GRANDFATHER_EXISTING

Semantic unique constraints#

A semantic unique constraint dedups by embedding similarity rather than exact value — two nodes are "the same" if their vectors are within a threshold, scoped to a partition property:

CREATE SEMANTIC UNIQUE CONSTRAINT fact_semantic_dedup
  ON :Fact(embedding)
  WITHIN PROPERTY workspaceId
  THRESHOLD 0.92
  • ON :Label(property) — the embedding property to compare.
  • WITHIN PROPERTY <prop> — the partition scope (uniqueness is enforced per distinct value of this property, e.g. per workspace).
  • THRESHOLD <0..1> — similarity at or above which two nodes are treated as duplicates.

This is how the engine prevents near-duplicate facts (paraphrases, re-ingested records) from accumulating, without an exact-match key.

See Also#

Try it
Open ↗
Loading engine…