Max Yari
Max Yari

Reputation: 3697

Unknown identifier error in nested FOREACH statement check

Looks like a simple error, but I'm unable to find anything specific about it, so hope you can shed some light on what is going on here.

Basically i'm trying to iterate through two sets of data, create elements for the first set and relate to them elements from the second set based on some condition. Condition check is also made via "foreach trick", therefore 2 levels of nested FOREACH's

FOREACH (plc IN [{name:'bar2',id:'pepe'},{name:'foo2',id:'papa'},{name:'foo3',id:'paps'}] |
   MERGE (place:Place {id:plc.id})
   ON CREATE SET place = plc

   FOREACH (evt IN [{placename:'foo2',id:'pewpew'},{placename:'foo2',id:'powpow'},{placename:'bar2',id:'pawpaw'},{placename:'bar2',id:'pwaaagh'},{placename:'foo3',id:'pwaaagh'}] |

      FOREACH (ignoreme in CASE WHEN evt.placename = plc.name THEN [1] ELSE [] END |
         CREATE (e:Event)-[rel:IN]->(place) 
         SET e=evt
)))

Happily giggling Neo4j throws at me Unknown identifier "e"

Upvotes: 2

Views: 96

Answers (2)

Brian Underwood
Brian Underwood

Reputation: 10856

I always suggest trying with UNWIND before FOREACH.

WITH
  [{name:'bar2',id:'pepe'},{name:'foo2',id:'papa'},{name:'foo3',id:'paps'}] AS plcs,
  [{placename:'foo2',id:'pewpew'},{placename:'foo2',id:'powpow'},{placename:'bar2',id:'pawpaw'},{placename:'bar2',id:'pwaaagh'},{placename:'foo3',id:'pwaaagh'}] AS evts
UNWIND plcs AS plc
MERGE (place:Place {id:plc.id})
WITH place, plc, evts
UNWIND evts AS evt
WITH place, plc, evt
WHERE evt.placename = plc.name
CREATE (e:Event)-[:IN]->(place)
  SET e=evt

Upvotes: 2

Michael Hunger
Michael Hunger

Reputation: 41706

Seems to be a bug. You can work around it, by splitting the longer create pattern into two, so that the e variable is created.

FOREACH (plc IN [{name:'bar2',id:'pepe'},{name:'foo2',id:'papa'},{name:'foo3',id:'paps'}] |
   MERGE (place:Place {id:plc.id})
   ON CREATE SET place = plc

   FOREACH (evt IN [{placename:'foo2',id:'pewpew'},{placename:'foo2',id:'powpow'},{placename:'bar2',id:'pawpaw'},{placename:'bar2',id:'pwaaagh'},{placename:'foo3',id:'pwaaagh'}] |

      FOREACH (ignoreme in CASE WHEN evt.placename = plc.name THEN [1] ELSE [] END |
         CREATE (e:Event)
         CREATE (e)-[rel:IN]->(place) 
         SET e=evt
)))

Upvotes: 1

Related Questions