1

Consider I have a node having Label L1 in Neo4j database. I need to put a constraint on all nodes having label L1 such that there shall be no outgoing edge from these nodes. So whenever user tries to add an outgoing edge from these nodes, a constraint violation occurs. Is it possible to add user defined constraints on nodes and relationships?

If yes, how can I put such constraint(s) on nodes/relationships using Neo4j Java API's?

Enrico
  • 108
  • 1
  • 1
  • 11

1 Answers1

1

According to the Neo4j constraints documentation there is no way to do this.

I think you should delegate this type of responsibility to the application layer. Alternatively, you can try achieve your goal writing your own user defined procedure.

Bruno Peres
  • 15,845
  • 5
  • 53
  • 89
  • If it cannot be done then what are the usages of the enums ConstraintType.NODE_PROPERTY_EXISTENCE and ConstraintType.RELATIONSHIP_PROPERTY_EXISTENCE? – Suvam Kumar Das Feb 16 '18 at 13:32
  • [NODE_PROPERTY_EXISTENCE](http://neo4j.com/docs/developer-manual/current/cypher/schema/constraints/#query-constraint-prop-exist-nodes): ensures that all nodes with a certain label have a certain property. [RELATIONSHIP_PROPERTY_EXISTENCE](http://neo4j.com/docs/developer-manual/current/cypher/schema/constraints/#query-constraint-prop-exist-rels): makes sure that all relationships with a certain type have a certain property. @SuvamKumarDas – Bruno Peres Feb 16 '18 at 13:35
  • Can these two enums be somehow used to create constraints which satisfy properties on node and relationships? – Suvam Kumar Das Feb 16 '18 at 13:57
  • @SuvamKumarDas Sorry, I did not understand. Are you talking about ensure that a given node has an specified property? – Bruno Peres Feb 16 '18 at 14:33
  • Not exactly. If RELATIONSHIP_PROPERTY_EXISTENCE makes sure that all relationships with a certain type have a certain property, then can it be used to create a constraint? E.g. can I use RELATIONSHIP_PROPERTY_EXISTENCE to ensure that a particular type of relation will only exist between nodes of a particular label say L1 and L2 and not between nodes of other labels? – Suvam Kumar Das Feb 16 '18 at 16:08
  • @SuvamKumarDas No, because labels are not properties of relationships. – Bruno Peres Feb 16 '18 at 16:44
  • @SuvamKumarDas a relationship has a start node, an end node, a type and, optionally, properties. These constraints are relative to relationship and node *properties* only. – Bruno Peres Feb 16 '18 at 17:26
  • Hi @SuvamKumarDas if this answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Bruno Peres Feb 23 '18 at 11:27