cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Number of relationships constraint

Hi,

I am a newbie and this question may be a bit silly but anyway.

Ok let's say that I have nodes defining "parents" and other nodes defining "children". Is there any way to force parents to have only one child?

The parent nodes may have other relationships as well but I want a constraint on the number of children.

Or another example being binary trees, how could I make it that nodes have at most two subnodes?

Thank you for your answers! 

1 ACCEPTED SOLUTION

you could try to implement something on your own, since there are no native capabilities as @dana_canzano pointed out.  You could look at implementing apoc triggers that does not allow your invalid scenarios. The trigger could look at the relationships added before the commit and throw an exception when your invalid condition is met. 

https://neo4j.com/labs/apoc/4.4/background-operations/triggers/

https://neo4j.com/labs/apoc/4.4/overview/apoc.util/apoc.util.validate/

note: I have not done this myself so it is a concept to investigate. Maybe others with experience can comment. 

 

View solution in original post

4 REPLIES 4

@reubenhayes 

version of Neo4j?

https://neo4j.com/docs/cypher-manual/current/constraints/   describes available constraint functionality for Neo4j 5, and there is not a constraint available to achieve what is desired

 

 

Hi @dana_canzano ,

neo4j version > 4.3.

I have looked the constraints page and I was wondering if there is another way, a workaround perhaps, available.

you could try to implement something on your own, since there are no native capabilities as @dana_canzano pointed out.  You could look at implementing apoc triggers that does not allow your invalid scenarios. The trigger could look at the relationships added before the commit and throw an exception when your invalid condition is met. 

https://neo4j.com/labs/apoc/4.4/background-operations/triggers/

https://neo4j.com/labs/apoc/4.4/overview/apoc.util/apoc.util.validate/

note: I have not done this myself so it is a concept to investigate. Maybe others with experience can comment. 

 

@glilienfield ,

Thank you. I will definitely check the apoc triggers.