using Neo4j 3.0.4
Want to conditionally create vertices if there is an optional match on a path:
with [
{customer: 0001, created: 1474434591, version: "1.0.0"},
{customer: 0001, created: 1474434500, version: "1.0.0"},
{customer: 0003, created: 1474445743, version: "1.1.0"}
] as lines
unwind lines as line
/// customers above already exist
MATCH (c:Customer {id: line.customer})
OPTIONAL MATCH (c)-[:HAS]->(co:Configuration) WHERE co.created >= line.created
WITH CASE WHEN co IS NULL THEN
[1]
END as createInventory,
CASE WHEN co IS NOT NULL THEN
[count(co)]
END as throwError, c, line
FOREACH (x in createInventory |
CREATE (n:Configuration {
created: line.created,
version: line.version
})
CREATE UNIQUE (c)-[:HAS]->(n)
)
FOREACH (x in throwError |
CREATE (e:Error:Inventory { message: "Configuration " + line.created + " >= existing (" + x + ")" })
);
The expectation is that only 2 Configuration
vertices would be created and associated with a Customer
. Instead, all 3 Configuration
instances are created. The write statements (i.e. create) are within the same transaction so I assume that individual writes aren't reflected when looping through the OPTIONAL MATCH
. In this example, a row is a Configuration
and I don't want to create new ones tied to the same Customer
if it's created property is older than an existing. Thus the predicate in the OPTIONAL MATCH
.
Any way to conditionally create and take into account writes within the same transaction (or Cypher statement)?