0

When I execute the query CREATE (p:Person)-[s:SPEAKS]-(l:Language) RETURN p, s, l;, I get the following error:

Error: Query failed: Bidirectional relationship are not supported when creating an edge

If I use MERGE instead of CREATE, the relationship is created:

MERGE (p:Person)-[s:SPEAKS]-(l:Language) RETURN p, s, l;

Why is this happening?

MasaZo
  • 43
  • 5
  • 1
    Does this answer your question? [When executing the CREATE query I get the error about Bidirectional relationship](https://stackoverflow.com/questions/75548537/when-executing-the-create-query-i-get-the-error-about-bidirectional-relationship) – Stefan - brox IT-Solutions Mar 24 '23 at 09:40

2 Answers2

1

The simple answer is that the Cypher language says that MERGE can accept an undirected relationship pattern.

But there is a subtle reason why MERGE must accept undirected relationship patterns.

When you use CREATE to create an undirected relationship, you don't care if there already are existing matching relationships in either direction. So you can just specify an arbitrary direction for the new relationship.

On the other hand, when you use MERGE to create an undirected relationship only if one does not already exist in either direction, you cannot safely specify an arbitrary direction for the new relationship. That is because if there happened to be an existing relationship in the opposite direction, then the MERGE would inappropriately create a new relationship, resulting in 2 relationships when you only wanted 1.

So MERGE must support undirected relationship patterns, but CREATE does not.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Thanks! I guess that some internal mechanism decides about the direction of the relationship that MERGE will create? – MasaZo Mar 27 '23 at 12:26
0

When executing the query CREATE (p:Person)-[s:SPEAKS]-(l:Language) RETURN p, s, l;, you get the following error:

Error: Query failed: Bidirectional relationship are not supported when creating an edge

If you use MERGE instead of CREATE, the relationship is created:

MERGE (p:Person)-[s:SPEAKS]-(l:Language) RETURN p, s, l;

I ran those same queries in Neo4j and they produce the same results as memgraph does - CREATE fails, MERGE succeeds. The cypher spec also says that in case of MERGE, a direction is picked even if none was specified.

To quote the spec: "A property graph may be defined in graph theoretical terms as a directed, vertex-labeled, edge-labeled multigraph with selfedges, where edges have their own identity." - so, from this point it makes sense that CREATE behaves like it does.

Taja Jan
  • 942
  • 1
  • 1
  • 11