neo4j/cypher-dsl

Invalid cypher genearated for merge in nested subquery

Closed this issue · 1 comments

Given:

var n1 = Cypher.node("Foo").named("n1");
var  n2 = Cypher.node("Bar").named("n2");
var resultStatement = Cypher
	.create(n1)
	.with(n1)
	.call(
		Cypher.with(n1)
			.merge(n2.withProperties("foo", Cypher.literalOf("x")))
			.create(n1.relationshipTo(n2, "NESTED"))
			.returning(Functions.count(n2).as("foo_2"))
			.build()
	)
	.returning(Cypher.literalTrue())
	.build();

The following cypher is generated:

CREATE (n1:Foo)
  WITH n1
  CALL {
    WITH n1
    MERGE (n2:Bar {
      foo: 'x'
    })
    CREATE (n1)-[:NESTED]->(n2:Bar)
    RETURN count(n2) AS foo_2
  }
  RETURN true

I would expect the following cypher:

CREATE (n1:Foo)
  WITH n1
  CALL {
    WITH n1
    MERGE (n2:Bar {
      foo: 'x'
    })
    CREATE (n1)-[:NESTED]->(n2)
    RETURN count(n2) AS foo_2
  }
  RETURN true

Note the CREATE (n1)-[:NESTED]->(n2) without label for n2

Neo4j will throw an error like:

Can't create node n2 with labels or properties here. The variable is already declared in this context

Thanks @Andy2003 This is a bug and will be fixed soon.

In the meantime, you can work around by defining your query as so:

var n1 = Cypher.node("Foo").named("n1");
var  n2 = Cypher.node("Bar").named("n2").withProperties("foo", Cypher.literalOf("x"));
var resultStatement = Cypher
	.create(n1)
	.with(n1)
	.call(
		Cypher.with(n1)
			.merge(n2)
			.create(n1.relationshipTo(n2, "NESTED"))
			.returning(Functions.count(n2).as("foo_2"))
			.build()
	)
	.returning(Cypher.literalTrue())
	.build();