Bill L
Bill L

Reputation: 2836

How to conditionally drop a constraint in cypher

I am trying to query for a specific constraint, and if it exists, drop it, otherwise I want to do nothing and move on. I am attempting to do so with the following code:

SHOW CONSTRAINTS YIELD name, labelsOrTypes, properties
WHERE labelsOrTypes = ["NodeLabel"] AND properties = ["targetProperty"]
WITH name
CALL apoc.when(
    name IS NOT NULL,
    'DROP CONSTRAINT name',
    '',
    {name: name}
) YIELD value;

This gives an error Invalid input 'WITH'. Removing the WITH gives Invalid input 'CALL'. Returning the name property gives the same error.

Neo4J lists in their documentation that "One of the output columns from SHOW CONSTRAINTS is the name of the constraint. This can be used to drop the constraint with the DROP CONSTRAINT command." Removing the APOC call and doing the following also results in an error:

SHOW CONSTRAINTS YIELD name, labelsOrTypes, properties
WHERE labelsOrTypes = ["PublicToken"] AND properties = ["token"]
DROP CONSTRAINT name

This tells me Invalid input 'd', so it seems you can't use DROP CONSTRAINT immediately after showing constraints. What am I missing? Is this command just not able to be used in a script and has to be used manually?

Upvotes: 0

Views: 158

Answers (1)

Charchit Kapoor
Charchit Kapoor

Reputation: 9284

Yes, SHOW CONSTRAINTS is a command, and we can't use it as a subquery or pipeline its output, along with other Cypher clauses, to achieve some functionality.

This documentation gives the complete syntax for SHOW CONSTRAINTS, as you can see WITH is not supported. For the command, YIELD acts as RETURN, if no return statement is present. So, you will have to run DROP CONSTRAINT, manually after your constraints are listed,

Upvotes: 2

Related Questions