cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

"merge - on create - on match" with constrained node property key behaves different when applied in browser client or java driver

Database: NEO4J CE 3.5.11 (also reproduced on NEO4J EE 3.5.11)
Driver: 4.0.0-beta02
Java Version: openjdk 11

I am trying to apply "MERGE / ON CREATE SET / ON MATCH SET" logic to Nodes decorated with a uniqueness constraint on one property. Using the browser client, I'm able to merge & match the node, but using the java driver I get this

org.neo4j.driver.exceptions.ClientException: Node(78251) already exists with label MyNode and property keyprop = '1001632'

What does the Browser Client do the Java Driver doesn't?

1) Background / "nano data model"

The "MyNode" stoods for a Node value, whose properties spring from a "daily" dataset. The dataset may differ from the previous days dataset in that it contains properties for additional "MyNode", does not contain properties of a certain "MyNode" any longer, does contain changed properties for a "MyNode". Each MyNode instance is uniquely identified by a property.

2) Demo

Please find some sample code for playing around with the issue below.

3) Code Precondition (Constraint, Anchor-Node)

CREATE CONSTRAINT ON (n:MyNode) ASSERT n.keyprop IS UNIQUE
MERGE (an:Anchor {key: "Anchor-01"})

4) Code called once and subsequently

MATCH (an:Anchor {key: "Anchor-01"})
MERGE (an)<-[:ANCHORS]-(m:MyNode {keyprop: "key1234"})
ON CREATE SET
m.name = "m1234" ,
m.etc = "etc. pp." ,
m.created = timestamp()
ON MATCH SET
m.name = "m1234" ,
m.etc = "etc. pp." ,
m.seen = timestamp()

2 REPLIES 2

MuddyBootsCode
Graph Steward

Have you looked at your instance to see if possibly that node actually does already exist? Maybe on the desktop? It could possibly be an order of operations type thing. Maybe change the Cypher query to:

MATCH (an:Anchor {key: "Anchor-01"})
MERGE (m:MyNode {keyprop: "key1234"})
MERGE (an)<-[:ANCHORS]-(m)
...

See if that gives you the same error? That's assuming you're trying to create that MyNode in that MERGE step.

anthapu
Graph Fellow

MERGE (an)<-[:ANCHORS]-(m:MyNode {keyprop: "key1234"})

MERGE tries to match the pattern and if any part of the pattern does not exist, it will re-create the whole pattern.

In this case If MyNode with keyprop:"key1234" does not exist OR if there is no "ANCHORS" relationship does not exist between the nodes, it tries to create new MyNode and ANCHORS relation.

So, if a node already exists with that keyprop, if the relationship does not exist it tries to create the node.

To avoid this you should try the way Michael showed above.

MATCH (an:Anchor {key: "Anchor-01"})
MERGE (m:MyNode {keyprop: "key1234"})
MERGE (an)<-[:ANCHORS]-(m)