Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-15-2020 04:16 PM
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?
Solved! Go to Solution.
05-15-2020 04:30 PM
Hello,
You can get database constraints with this command CALL db.constraints()
, you just need after to compare strings 🙂
05-15-2020 04:30 PM
Hello,
You can get database constraints with this command CALL db.constraints()
, you just need after to compare strings 🙂
10-04-2020 04:44 PM
I must say - this is a frustrating change... makes for a LOT more lines of code.
I'm trying to follow the logic to write a validation check (just to add a constraint) - I need to CALL db.contraints() Find if there is a match, but only continue on if there isn't a match, and rinse-repeat for EVERY constraint I want to create.
Could you provide an example code to do this? Is there a function that makes this easier to accomplish?
I'm still a bit baffled by this change.
10-04-2020 06:52 PM
Have a look at apoc.schema.assert() to see if that would work for you. Be aware that the default for dropExisting is true and it will drop all existing indexes/constraints that are not included in the command.
10-05-2020 10:49 AM
I was still drafting my reply when I saw the reference to assert. Is anyone surprised that it was APOC to the rescue?
CALL apoc.schema.assert(null,{Assetcategory:['name']},False)
This works VERY well.
10-05-2020 10:53 AM
Actually works BETTER using APOC, because you can list MULTIPLE labels/properties within ONE apoc CALL, so you can perform your indexes/constraints in FEWER lines of code than native Cypher.
10-04-2020 11:43 PM
Hello @pdrangeid
Yeah I found this very frustrating as well Which language are you using?
Regards,
Cobra
All the sessions of the conference are now available online