Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-27-2022 01:01 PM
Quick question:
Is there a way to limit the properties that can be added to a node with a certain label? I assume that would be listed in the constraints if it was possible. It seems like there is nothing stopping a developer from spelling the name of a property wrong in a cypher query and adding that wrong property to a node. https://neo4j.com/docs/cypher-manual/current/constraints/
10-27-2022 09:42 PM - edited 10-27-2022 09:44 PM
Hi graphene,
I don't think this is possible in cypher like it is in sql. But this is kinda wanted because the schema isn't fixed. I have 2 suggestions on how to get around it, if you actually want to restrict the developer from making mistakes:
1. If you're working in Neo4j Browser, you could add all properties of nodes or node types into parameters and use the parameters such that there is a warning if you misspelled the parameter name. See Parameters - Neo4j Cypher Manual for more information.
2. If you're working e.g. with Java and writing user-defined procedures, we came to the conclusion that it makes sense to write descriptors of the data that you expect and want to have in your Neo4j db. Then, when you want to set / get properties on nodes you would not write
Node tomhanks = tx.findNode(Label.label("Person"),"name", "Tom Hanks");
long born = (long) tomhanks.getProperty("born",0);
but rather
Node tomhanks = tx.findNode(Label.label("Person"),PersonNode.getNamePropertyKey(), "Tom Hanks");
long born = (long) tomhanks.getProperty(PersonNode.getBornPropertyKey(),0);
That way, you can be safer that the property exists and it's also refactoring safe, i.e. if you change the property name in the database at one point you only have to change the key in the descriptors once and can keep all other code.
I hope I could help you.
Elena
All the sessions of the conference are now available online