Neo4J Cypher: Ensuring constraints exist (avoiding errors if they already do)

Viewed 1189

On app startup I'd like to ensure indicies and constraints exist.

When I call the following on app startup, I'm receiving an error as the constraint already exists:

CREATE CONSTRAINT ON (node:MyLabel) ASSERT node.myProperty IS UNIQUE

I'd like to ensure that the constraint exists without throwing errors.

Is there a way to do this in Cypher? I'm struggling to find any help in the Neo4J documentation.

3 Answers

You can use IF NOT EXISTS, for example: CREATE CONSTRAINT IF NOT EXISTS ON (node:MyLabel) ASSERT node.myProperty IS UNIQUE

Related