How to check a constraint already exists in Neo4J 4.0

constraint_command = "CREATE CONSTRAINT ID ON (n:" + label + ") ASSERT n.ID IS UNIQUE"

My database may already have such a constraint. Before my program executes this statement, I need a way to tell whether it already had this constraint. It seems in Neo4j 3.5, it didn't complain even if it already exits. But in 4.0, it gives this error message:

neobolt.exceptions.ClientError: An equivalent constraint already exists, 'Constraint( UNIQUE, :Product(ID) )'.

How to check before creating it?

Hello,

You can get database constraints with this command CALL db.constraints(), you just need after to compare strings :)

1 Like