# Is there an inbuilt mechanism to control relationship cardinality?

**URL:** <https://community.neo4j.com/t/is-there-an-inbuilt-mechanism-to-control-relationship-cardinality/61461>\
**Category:** Cypher\
**Tags:** relationship, cardinality, multiplicity\
**Created:** [March 16, 2023, 3:04am UTC](https://community.neo4j.com/t/is-there-an-inbuilt-mechanism-to-control-relationship-cardinality/61461 "2023-03-16T03:04:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcantoni](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pcantoni](https://community.neo4j.com/u/pcantoni)\
**Post date:** [March 16, 2023, 3:04am UTC](https://community.neo4j.com/t/is-there-an-inbuilt-mechanism-to-control-relationship-cardinality/61461/1 "2023-03-16T03:04:46Z")

</div>

Although a newbie to Neo4j and cypher, I first built an in-memory, dynamic-schema, graph database over 4 decades ago (not industrial scale - just a postgrad project). One really neat feature was the ability to specify the multiplicity of the binary relationship (similar to UML). For example, using "bastardised cypher"  
(:Person)-[:HAS\_BIOLOGICAL\_PARENT]-\> [2:2] (:Person)  
A person may only have two and only two biological parents (who are persons).  
When you tried to add the third parent, the DBMS would throw an exception and not allow it!  
Is there any inbuilt mechanism in Neo4j/cypher to do this or, if I want to do something similar, I'd have to "roll my own"?

TIA,  
Paolo

---

<div class="post-metadata">

**Author:** ![lyonwj](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lyonwj/32/27358_2.png) [@lyonwj](https://community.neo4j.com/u/lyonwj)\
**Post date:** [March 16, 2023, 6:13pm UTC](https://community.neo4j.com/t/is-there-an-inbuilt-mechanism-to-control-relationship-cardinality/61461/2 "2023-03-16T18:13:24Z")

</div>

Nothing built-in to enforce that constraint that I've seen. Although some of the integrations for Neo4j support this. For example, in the [Neo4j GraphQL Library](https://neo4j.com/docs/graphql-manual/current/type-definitions/relationships/#_cardinality) and [Neosemantics](https://neo4j.com/labs/neosemantics/4.0/validation/).

The way to enforce this at the database level would be through the use of a [transaction event handler](https://neo4j.com/docs/java-reference/current/transaction-management/#transactions-events) or perhaps by [defining an APOC trigger](https://neo4j.com/labs/apoc/4.4/background-operations/triggers/) to check that the cardinality constriant isn't violated by the transaction.

---

<div class="post-metadata">

**Author:** ![pcantoni](https://avatars.discourse-cdn.com/v4/letter/p/898d66/32.png) [@pcantoni](https://community.neo4j.com/u/pcantoni)\
**Post date:** [March 16, 2023, 10:10pm UTC](https://community.neo4j.com/t/is-there-an-inbuilt-mechanism-to-control-relationship-cardinality/61461/3 "2023-03-16T22:10:20Z")

</div>

Thanks, William @lyonwj; I'll check them out.

Paolo
