memgraphdb

How to create a Constraint that has multiple labels in Memgraph?


I'm trying to create a constraint that encompasses multiple labels, which seems to be unsupported, or I'm missing the correct approach.

For context, creating a constraint with a single label works perfectly fine, like so:

CREATE CONSTRAINT ON (n:Employee) ASSERT EXISTS (n.first_name);

However, my requirement involves multiple labels for a node, and my attempt to create a similar constraint fails:

CREATE CONSTRAINT ON (n:Employee:Teacher) ASSERT EXISTS (n.first_name);

This attempt results in an error indicating a syntax issue:

Query failed: line 1:33 no viable alternative at input '(n:Employee:' Is there a syntax or method in Memgraph that allows for creating constraints across multiple labels?


Solution

  • You can create a constraint on the Employee and on a Teacher node, if it is Teacher it will have a constraint, if it is Employee it will have constraint, but you have to do it with the following two lines:

    CREATE CONSTRAINT ON (n:Employee) ASSERT EXISTS (n.first_name);
    CREATE CONSTRAINT ON (n:Teacher) ASSERT EXISTS (n.first_name);