How to create unique CONSTRAINT to relationship by neo4j cypher?

If I understood your problem correctly, you want to enforce uniqueness of certain kind of relation rather than uniqueness of relation's certain attribute. If that's what you want, then you enforce such uniqueness by using "CREATE UNIQUE":

MATCH (root { name: 'root' })
CREATE UNIQUE (root)-[:LOVES]-(someone)
RETURN someone

The Neo4j Manual: Create unique relationships


At present, there is only one kind of CONSTRAINT neo4j will let you create, and that's a UNIQUENESS constraint. That link cites what's in the internal API, and you'll notice there's only one type at present.

Here's a link on how to create a uniqueness constraint.

This lets you assert that a certain property of a node must be unique, but it doesn't say anything about relationships. I don't think it's possible to constrain what sort of relationships can come off of various nodes.


it seems that a relationship constraint can only enforce the existence of a relationship property but not its uniqueness

CREATE CONSTRAINT ON ()-[like:LIKED]-() ASSERT exists(like.day)

http://neo4j.com/docs/developer-manual/current/cypher/#query-constraints-prop-exist-rels