Pouria
Pouria

Reputation: 431

Duplicated nodes and relationships

I have a function that creates a VISIT node each time a link gets visited. I also attach some stats to this visit node, like DATE, BROWSER, OS etc.

But sometimes the Date node (or probably other nodes too) gets duplicated, meaning that I get 2 date nodes for one VISIT.

Each VISIT should only be attached to one stats node. (e.g. one browser, one date)

Here's my code:

MATCH (l:URL { id: $id })
CREATE (v:VISIT)
MERGE (b:BROWSER { browser: $browser })
MERGE (c:COUNTRY { country: $country })
MERGE (o:OS { os: $os })
MERGE (r:REFERRER { referrer: $referrer })
MERGE (d:DATE { date: $date })
MERGE (v)-[:VISITED]->(l)
MERGE (v)-[:BROWSED_BY]->(b)
MERGE (v)-[:LOCATED_IN]->(c)
MERGE (v)-[:OS]->(o)
MERGE (v)-[:REFERRED_BY]->(r)
MERGE (v)-[:VISITED_IN]->(d)
RETURN l

And here is the graph image example, VISIT (red node) should only have one VISITED_IN relationship and to only one node of that type.

duplicated nodes imag

Upvotes: 0

Views: 79

Answers (1)

Muldec
Muldec

Reputation: 4901

Merge is not ThreadSafe. It basically does 3 thing in a row, but can run into concurrency issues : 1. Check if the pattern exists 2. Creates it if it does not 3. Returns it

If two merge requests come with the same pattern at the same time, it does not guarantee that only one will be created.

You should couple your merge request with a unique constraint to ensure uniqueness

CREATE CONSTRAINT ON (d:Date) ASSERT d.date IS UNIQUE

Upvotes: 1

Related Questions