Search code examples
memgraphdbopencypher

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


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.


Solution

  • 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.