The context is an HTTP API server which auto-generates the Cypher queries (Equill/restagraph: App that dynamically generates REST-ish APIs for a Neo4j database, using a schema defined within the database. - Codeberg.org), and I promise this approach makes sense in the context of the API's semantics.
I can create the resources just fine:
CREATE (:parent {uid: "foo"})-[:PARENT_CHILD]->(:child {uid: "bar"})-[:CHILD_GRANDCHILD]->(:grandchild {uid: "baz"});
Creating back-links from child to parent, and from grandchild to child, works fine in the traditional manner using a hand-crafted query
MATCH (p:parent {uid: "foo"})-[:PARENT_CHILD]->(c:child {uid: "bar"}) CREATE (c)-[:CHILD_PARENT]->(p);
MATCH (:parent {uid: "foo"})-[:PARENT_CHILD]->(c:child {uid: "bar"})-[:CHILD_GRANDCHILD]->(g:grandchild {uid: "baz"}) CREATE (g)-[:GRANDCHILD_CHILD]->(c);
If I check these with the following queries, I get the expected output:
MATCH (p:parent {uid: "foo"})-[:PARENT_CHILD]->(c:child {uid: "bar"})-[:CHILD_PARENT]->(z) RETURN z;
MATCH (p:parent {uid: "foo"})-[:PARENT_CHILD]->(c:child {uid: "bar"})-[:CHILD_GRANDCHILD]->(:grandchild {uid: "baz"})-[:GRANDCHILD_CHILD]->(z) RETURN z;
However, that isn't how the server generates this code. It doesn't check whether the target of a new relationship can be reached via a subset of the source's path, so it generates both paths from scratch. Because of this, it tries to create those relationships with suboptimal Cypher queries:
MATCH (p:parent {uid: "foo"}), (:parent {uid: "foo"})-[:PARENT_CHILD]->(c:child {uid: "bar"}) CREATE (c)-[:CHILD_PARENT]->(p);
MATCH (:parent {uid: "foo"})-[:PARENT_CHILD]->(c:child {uid: "bar"}), (:parent {uid: "foo"})-[:PARENT_CHILD]->(:child {uid: "bar"})-[:CHILD_GRANDCHILD]->(g:grandchild {uid: "baz"}) CREATE (g)-[:GRANDCHILD_CHILD]->(c);
The odd thing here is that the first one (depth=2 to depth=1) works fine. The second one (depth=3 to depth=2) fails. It returns the first two of the usual lines:
0 rows
ready to start consuming query after 457 ms, results consumed after another 0 ms
However, the expected third line "Created 1 relationships" doesn't appear. When I test with the previous queries ending in "RETURN z", the first returns the requested property, but the second reports that this relationship doesn't exist.
My question is whether this should succeed and I've found a bug, or whether I'm doing it wrong and should find another approach. I've verified this behaviour with versions 4.4.18 and 5.21.0.