neo4j/community — Issue #202: Support 1:N and 1:1 Constraints
Author: ractive
Created: 2012-01-27 08:43:20+00:00
State: open

Neo4j should support constraints on relationships. It's e.g. a common requirement that there only must exist one relationship of one type in one direction between two nodes to support 1:N, N:1 or 1:1 relationships.

There is no way to make sure that a node e.g. has only one relationship of a given type to another node (1:N or N:1 relationship) at the moment . You can of course check if there already exists such a relationship before creating one, but you cannot make sure that another transaction in parallel just creates another one as well. You can only check again after committing the current transaction - which makes it impossible to roll it back if a violation is detected. Neo4j itself must support the detection of such constraint violations when committing the current transaction. Client code cannot detect it reliably.

Maybe something like this would be enough:

```java
try {
nodeA.createRelationshipTo(nodeB, REL, Constraints.SINGLE)
} catch(ConstraintViolation e) {
.....
}
```

PS: This is a follow-up to [this discussion](https://groups.google.com/forum/#!topic/neo4j/xEBTkLMctUA) on the neo4j google gropu.