I have designed a Cypher query that:
- - checks if a parent node has a child node of certain relationship, if not creates child node
- - checks if the parent node has a child node where property does not match new data, if so it creates a new child node
- - returns the old child node, if existed
- - returns the new node
My problem is when I invoke this when no prior child node existed, sometimes it returns the newly created node as the old child node object, sometimes it returns what is expected: null.
Looking at the query I cannot see how this could happen, unless somehow the query is executed twice, first time creating the new node, 2nd time seeing the newly created node as the old child node.
I wanted to ask if anyone can see a reason for this, perhaps there is an error in my query?
MATCH (parent:rateTable { rateTableId: "test" } )
OPTIONAL MATCH (parent)-[currentRel: current_rateTableRates ]->(currentVersion)
FOREACH(ignoreMe IN CASE WHEN currentRel IS NULL THEN [1] ELSE [] END |
MERGE (newVersion:rateTableRates { rate: 10, forMatchedId: "xx" } )
)
WITH currentVersion, currentRel
FOREACH(ignoreMe IN CASE WHEN currentRel IS NOT NULL AND currentVersion IS NOT NULL AND currentVersion.forMatchedId <> "xx" THEN [1] ELSE [] END |
MERGE (newVersion:rateTableRates { rate: 10, forMatchedId: "xx" } )
)
WITH currentVersion
OPTIONAL MATCH (newVersion:rateTableRates { rates: 10, forMatchedId: "xx" } )
RETURN currentVersion, newVersion