Fix: Enforce cardinality on RelationshipFrom and bidirectional relationships#892
Conversation
cee33ff to
48520c6
Compare
…trib#891 - following contributing instructions plus tests
05878e0 to
b47f87f
Compare
|
|
Tagging top contributors for visibility @robinedwards @aanastasiou @mariusconjeaud @pkatseas @tonioo |
|
Hello ! Thanks a ton for this very well documented and explained Issue & PR ! And also for the thorough research and linking with similar issues. I have been away from neomodel issues for the past few months, so apologies for the delay in reviewing this. |
|
@mariusconjeaud thanks for the approval and merge! 🥳 On reflection, this could be a breaking change for unsuspecting users that are using Specifically, applications that currently connect multiple nodes through a Nevertheless, since developers explicitly declare cardinality in their model, it’s reasonable they should expect enforcement. The fix makes the API consistent and aligns with the documented contract, but I wanted to flag the behavioural change so it’s not surprising to users. |
|
my bad, I have just caught up with everything and read your comment here. You're already way ahead of me 😅 Thanks for resolving 🙌 |



Context
This pull request addresses a critical data integrity issue where cardinality constraints on
RelationshipFromwere not being enforced, leading to potential inconsistencies in the graph data. This change introduces bidirectional cardinality validation to ensure that relationship constraints are respected from both the source and target nodes' perspectives.Problem
Previously, neomodel only validated the cardinality of a relationship from the source side (the node on which the
.connect()method is called). This meant that aRelationshipTowith a specific cardinality (One,ZeroOrOne) would be correctly validated, but the correspondingRelationshipFromon the target node would not be.For example, if a
Petcould only have oneOwner(RelationshipFrom('Owner', 'OWNS', cardinality=One)), it was still possible to connect multipleOwnernodes to the samePet, violating the constraint without raising an error.This fixes issues #464 and #867 and #891
Solution
The core of the solution is to enforce cardinality checks on both ends of a relationship during the
connectoperation.Bidirectional validation in
connect:connectmethod inRelationshipManagerhas been updated to perform two checks:_check_cardinalitymethod:_check_cardinality, has been introduced to the baseRelationshipManager.One,ZeroOrOne) to encapsulate the validation logic.connectmethods of the cardinality classes into this new method, centralising the logic.How to verify
A test case has been added to confirm the fix. The following scenario now works as expected:
Impact
RelationshipFromcardinality. Code that created relationships violating these constraints will now raise anAttemptedCardinalityViolation. This is the correct and expected behaviour.