Is it possible to constraint edge multiplicity in Neo4j / OrientDB?
Asked Answered
K

2

6

I was wondering whether Neo4j and OrientDB provide the possibility of defining constraints in terms of multiplicity for specific edge types?

Kilocalorie answered 9/10, 2014 at 9:0 Comment(0)
Z
7

For OrientDB

You can set multiplicity on out/in collection per label. Example to set maximum 1 edges from Person to Company if the edge's label is "workFor":

ALTER PROPERTY Person.out_workFor NOT NULL
ALTER PROPERTY Person.out_workFor MAX 1

You can also set the minimum with:

ALTER PROPERTY Person.out_workFor MIN 1

In this way with min & max it's mandatory having one and only one relationship to Company.

Zeppelin answered 9/10, 2014 at 12:30 Comment(1)
I keep getting a 'property not exists' error when trying to create a constraint using this method.Acord
D
-1

For Neo4j

What do you want to do happen when the constraint is violated? Exception & Rollback or somehow merge?

For 1:1 cardinality there is merge

MATCH (p:Person {name:"Pablo"})
MATCH (c:Company {name:"Era7"})
MERGE (p)-[:WORKS_FOR]->(c);

For higher cardinality constraints, you can either go with a framework that supports meta-models and schema like structr.org or sylvadb.

Or you can set-up a small tx-event handler that checks your cardinalities and raises an exception if you violate the constraints.

I wanted to write up a blog post about it anyway, so stay tuned.

Disentomb answered 9/10, 2014 at 9:52 Comment(3)
What do you mean by merging? I guess I would simply expect it not to create the relationship that does not fulfill the constraint previously expressed somehow when defining the schema.Kilocalorie
I stared to provide a project that implements the suggestion: github.com/jexp/neo4j-constraintsDisentomb
Any idea if the behavior I previously described will be included in the official version at some point?Kilocalorie

© 2022 - 2024 — McMap. All rights reserved.