GRAPH.CONSTRAINT CREATE
syntax: | GRAPH.CONSTRAINT CREATE key MANDATORY|UNIQUE NODE label | RELATIONSHIP reltype PROPERTIES propCount prop [prop…]
—
Creates a graph constraint.
Introduction to constraints
A constraint is a rule enforced on graph nodes or relationships, used to guarantee a certain structure of the data.
FalkorDB supports two types of constraints:
- Mandatory constraints
- Unique constraints
Mandatory constraints
A mandatory constraint enforces existence of given attributes for all nodes with a given label or for all edges with a given relationship-type.
Consider a mandatory constraint over the attribute id of all nodes with the label Person. This constraint will enforce that any Person node in the graph has an id attribute. Any attempt to create or modify a Person node, such that the resulting node does not have an id attribute, will fail.
Unique constraints
A unique constraint enforces uniqueness of values of a given set of attributes for all nodes with a given label or for all edges with a given relationship-type. I.e., no duplicates are allowed.
Consider a unique constraint over the attributes: first_name and last_name of all nodes with the label Person This constraint will enforce that any combination of first_name, last_name is unique. E.g., a graph can contain the following Person nodes:
(:Person {first_name:'Frank', last_name:'Costanza'})
(:Person {first_name:'Estelle', last_name:'Costanza'})
But trying to create a third node with first_name Frank and last_name Costanza, will issue an error and the query will fail.
Creating a constraint
To create a constraint, use the GRAPH.CONSTRAINT CREATE command as follows:
GRAPH.CONSTRAINT CREATE key constraintType {NODE label | RELATIONSHIP reltype} PROPERTIES propCount prop [prop...]
Required arguments
key
is key name for the graph. constraintType
is the constraint type: either `MANDATORY` or `UNIQUE`. NODE label | RELATIONSHIP reltype
is the graph entity type (`NODE` or `RELATIONSHIP`) and the name of the node label or relationship type on which the constraint should be enforced. propCount
is the number of properties following. Valid values are between 1 and 255. prop...
is a list of `propCount` property names. Return value
@simple-string-reply - PENDING if executed correctly and the constraint is being created asynchronously, or @error-reply otherwise.
Examples
Creating a unique constraint for a node label
To create a unique constraint for all nodes with label Person enforcing uniqueness on the combination of values of attributes first_name and last_name, issue the following commands:
from falkordb import FalkorDB
client = FalkorDB()
graph = client.select_graph('g')
graph.query("CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)")
result = client.create_constraint('g', 'UNIQUE', 'NODE', 'Person', ['first_name', 'last_name'])
print(result)
import { FalkorDB } from 'falkordb';
const client = await FalkorDB.connect();
const graph = client.selectGraph('g');
await graph.query("CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)");
const result = await client.createConstraint('g', 'UNIQUE', 'NODE', 'Person', ['first_name', 'last_name']);
console.log(result);
let client = FalkorDB::connect_default();
let graph = client.select_graph("g");
graph.query("CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)")?;
let result = client.create_constraint("g", "UNIQUE", "NODE", "Person", &["first_name", "last_name"])?;
println!("{}", result);
FalkorDB client = new FalkorDB();
Graph graph = client.selectGraph("g");
graph.query("CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)");
String result = client.createConstraint("g", "UNIQUE", "NODE", "Person", Arrays.asList("first_name", "last_name"));
System.out.println(result);
redis> GRAPH.QUERY g "CREATE INDEX FOR (p:Person) ON (p.first_name, p.last_name)"
redis> GRAPH.CONSTRAINT CREATE g UNIQUE NODE Person PROPERTIES 2 first_name last_name
# Output: PENDING
Creating a mandatory constraint for a relationship type
To create a mandatory constraint for all edges with relationship-type Visited, enforcing the existence of a date attribute, issue the following command:
result = client.create_constraint('g', 'MANDATORY', 'RELATIONSHIP', 'Visited', ['date'])
print(result)
const result = await client.createConstraint('g', 'MANDATORY', 'RELATIONSHIP', 'Visited', ['date']);
console.log(result);
let result = client.create_constraint("g", "MANDATORY", "RELATIONSHIP", "Visited", &["date"])?;
println!("{}", result);
String result = client.createConstraint("g", "MANDATORY", "RELATIONSHIP", "Visited", Arrays.asList("date"));
System.out.println(result);
redis> GRAPH.CONSTRAINT CREATE g MANDATORY RELATIONSHIP Visited PROPERTIES 1 date
# Output: PENDING
Listing constraints
To list all constraints enforced on a given graph, use the db.constraints procedure:
result = graph.ro_query("call db.constraints()")
print(result)
const result = await graph.ro_query("call db.constraints()");
console.log(result);
let result = graph.ro_query("call db.constraints()")?;
println!("{:?}", result);
ResultSet result = graph.ro_query("call db.constraints()");
System.out.println(result);
redis> GRAPH.RO_QUERY g "call db.constraints()"
# Output: ...