Search code examples
neo4jcypher

Unable to access variables in `WITH`/`UNWIND` clause, inserting array of IDs in Neo4j


It seems like any variable I define outisde the WITH and UNWIND clause is not available inside the clause.

I wonder why this query returns that owner is not defined.

MATCH (owner:User { id: 'b6bba33e-646a-46c2-80a2-4b6a6b5cece9' })
// Create one coaching that has two participants (WITH clause)
CREATE (coaching:Coaching {
  id: randomUUID(),
  subject: 'Testing coaching 1' ,
  notes: 'testing this new feature',
  createdAt: DateTime()
})<-[:COACHES]-(owner)
              

WITH ['e1d8aaef-8db6-4dc1-8f3e-1bce06463d04', '6c20b918-284c-42a9-bc6f-ab4f99a09f2f'] AS participants
UNWIND participants AS p
MATCH (participant:User { id: p })
// Create relationship participant PARTICIPATES in coaching
CREATE (coaching)<-[:PARTICIPATES]-(participant)

RETURN owner, coaching, p
Variable `owner` not defined (line 16, column 8 (offset: 536))
"RETURN owner, coaching, p"

Solution

  • I needed to add the variables to the WITH clause.

    ...
    WITH ['e1d8aaef-8db6-4dc1-8f3e-1bce06463d04', '6c20b918-284c-42a9-bc6f-ab4f99a09f2f'] AS participants, owner, coaching
    UNWIND participants AS p
    MATCH (participant:User { id: p })
    // Create relationship participant PARTICIPATES in coaching
    CREATE (coaching)<-[:PARTICIPATES]-(participant)
    ...