My database model has users and MAC addresses. A user can have multiple MAC addresses, but a MAC can only belong to one user. If some user sets his MAC and that MAC is already linked to another user, the existing relationship is removed and a new relationship is created between the new owner and that MAC. In other words, a MAC moves between users.
This is a particular instance of the Cypher query I'm using to assign MAC addresses:
MATCH (new:User { Id: 2 })
MERGE (mac:MacAddress { Value: "D857EFEF1CF6" })
WITH new, mac
OPTIONAL MATCH ()-[oldr:MAC_ADDRESS]->(mac)
DELETE oldr
MERGE (new)-[:MAC_ADDRESS]->(mac)
The query runs fine in my tests, but in production, for some strange reason it sometimes creates duplicate MacAddress
nodes (and a new relationship between the user and each of those nodes). That is, a particular user can have multiple MacAddress
nodes with the same Value
.
I can tell they are different nodes because they have different node ID's. I'm also sure the Value
s are exactly the same because I can do a collect(distinct mac.Value)
on them and the result is a collection with one element. The query above is the only one in the code that creates MacAddress
nodes.
I'm using Neo4j 2.1.2. What's going on here?
Thanks, Jan