4
votes

I want to replace a relationship, if it exists, with a new node and two new relationships. The query below tests if the relationship exists but it fails on the new node creation. The CREATE syntax works on its own but not nested within the FOREACH loop.

START s = node(1)
MATCH (u)-[r?:ROOT]->(s)
FOREACH (u in (CASE WHEN r<>NULL THEN [u] ELSE [] END):

CREATE (u)<-[:REL1]-(n {test:"test"})-[:REL2]->(s))

RETURN s

I get an error saying:

Unknown identifier n

1

1 Answers

1
votes

I tested the FOREACH part in combination with the CREATE statement on this small sample graph http://console.neo4j.org/r/976pas with the following query:

START s=node:node_auto_index(name='A') 
MATCH (u)-[r:ROOT]->(s)
FOREACH (u IN (CASE WHEN r<>NULL THEN [u] ELSE [] END ): 
CREATE (u)<-[:REL1]-(n { name:'TEST' })-[:REL2]->(s))
RETURN DISTINCT s

On the neo4j console this works well. It produces the following result as expected: http://console.neo4j.org/r/ekreri