What is the most correct way to create bidirectional relationship in Memgraph using Cypher?

58 views Asked by At

I tried to run the query

CREATE (p1:Person {name: 'Harry'}), (p2:Person {name: 'Anna'})
CREATE (p1)<-[r1:MARRIED_TO]->(p2)
RETURN r1;

and I got the error Bidirectional relationship are not supported when creating an edge.

I got the same error for

CREATE (p1:Person {name: 'Harry'}), (p2:Person {name: 'Anna'})
CREATE (p1)-[r:MARRIED_TO]-(p2)
RETURN r;
 

I also tried

CREATE (p1:Person {name: 'Harry'}), (p2:Person  {name: 'Anna'})
CREATE (p1)-[r:MARRIED_TO]->(p2)
CREATE (p1)<-[r:MARRIED_TO]-(p2)
RETURN r;

but then I got Redeclaring variable: r. error.

2

There are 2 answers

3
Kelvin Lawrence On BEST ANSWER

Try using

CREATE (p1:Person {name: 'Harry'}), (p2:Person  {name: 'Anna'})
CREATE (p1)-[r1:MARRIED_TO]->(p2)
CREATE (p1)<-[r2:MARRIED_TO]-(p2)
RETURN r1,r2;

They are going to be two distinct edges. An edge cannot point in more than one direction.

2
cybersam On

You cannot create "undirected" or "bidirectional" relationships, since all relationships have a single specific direction. But luckily you never have to, since you can just search for relationships without caring about their directionality.

For example, this will find all MARRIED_TO relationships between p1 and p2 without caring about the direction of the relationship:

MATCH (p1)-[r1:MARRIED_TO]-(p2)
...