Search code examples
memgraphdb

Nested FOREACH get converted to property maps


I have Memgraph 2.4.1 running using WSL2 on Windows 11. It appears that when FOREACH statements are nested, the transfer of node lists is not handled accurately. There seems to be an unexpected conversion taking place where the node lists are morphing into property maps.

My Cypher code is following:

MATCH (oldNode) DETACH DELETE oldNode;
FOREACH (iterator IN RANGE(0,100)| CREATE (newNode:NEW_NODE {iterator: iterator}));
MATCH (node)
WITH node
ORDER BY node.id
WITH COLLECT(node) AS nodeList
FOREACH (index IN RANGE(0, SIZE(nodeList) - 2) |
 FOREACH (firstNode IN [nodeList[index]] |
  FOREACH (secondNode IN [nodeList[index+1]] |
    FOREACH (dummy IN [1] |
     MERGE (firstNode)-[:newRelation]->(secondNode)))));
MATCH path=(startNode)-[relationship]->(endNode) RETURN path;

Instead, what Iobserve is the emergence of two new nodes, namely firstNode and secondNode. If I take out the most inner FOREACH loop, secondNode starts behaving as an already established node, while new nodes are instantiated for every single occurrence of firstNode. This situation leads me to the assumption that there seems to be a potential issue with transferring the variables from the outer to the inner loop.


Solution

  • This was a bug and it was fixed in Memgraph 2.5.2. Use newer version of Memgraph and you should be fine.