Search code examples
pythonneo4jcypherpy2neo

Declare a unique constraint for Neo4j relationships dependent on direction (ideally with py2neo)


Is there an alternative to py2neo's create_unique method for relationships that takes direction into account? Such that uniqueness === same nodes, same label, and same direction?

A solution for Py2neo would be ideal, but I wouldn't be opposed to switching to a more complete driver if there is one.

EDIT: seems like it's a functionality that doesn't exist in Cypher in the first place. I still think it would make a useful driver functionality.

EDIT 2: Cypher let's me use create_unique with direction:

MATCH (b { name: 'Bob' }), (a {name:'Alice'})
CREATE UNIQUE (b)-[r:SWIPED_RIGHT]->(a)
RETURN r

Would be lovely to be able to do the same without writing a raw cypher query.


Solution

  • By default in Py2neo all relationship are created "Outgoing". Use "http://py2neo.org/2.0/essentials.html#py2neo.Rev" for "Incoming" relationships.

    Example: - //Creates "Outgoing" Relationship Relationship(Node("FEMALE",name="A"),"FRIEND",Node("FEMALE",name="B"))

    //Creates "Incoming" Relationship Relationship(Node("FEMALE",name="A"),Rev("FRIEND"),Node("FEMALE",name="B"))