Neo4j:MERGE创建重复的节点 [英] Neo4j: MERGE creates duplicate nodes
问题描述
我的数据库模型包含用户和MAC地址.一个用户可以有多个MAC地址,但一个MAC只能属于一个用户.如果某些用户设置了他的MAC,并且该MAC已经链接到另一个用户,则将删除现有关系,并在新所有者和该MAC之间创建一个新关系.换句话说,MAC在用户之间移动.
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.
这是我用来分配MAC地址的Cypher查询的特定实例:
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)
该查询在我的测试中运行良好,但是在生产环境中,出于某些奇怪的原因,它有时创建重复的MacAddress
节点(以及用户与每个节点之间的新关系).也就是说,特定用户可以具有相同Value
的多个MacAddress
节点.
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
.
我可以说它们是不同的节点,因为它们具有不同的节点ID.我也确保Value
完全相同,因为我可以对它们进行collect(distinct mac.Value)
,结果是一个元素的集合.上面的查询是代码中唯一创建MacAddress
节点的查询.
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.
我正在使用Neo4j 2.1.2.这是怎么回事?
I'm using Neo4j 2.1.2. What's going on here?
谢谢, 扬
推荐答案
这是我从Neo4j的支持下得到的回应(强调我的观点):
This is the response I got back from Neo4j's support (emphasis mine):
我已经从我们的团队那里得到了一些反馈,并且目前知道这可以在没有约束的情况下发生. MERGE实际上是MATCH或CREATE-且这两个步骤在事务内独立运行.给定并发执行和读提交"隔离级别,两者之间存在竞争条件.
I got some feedback from our team already, and it's currently known that this can happen in the absence of a constraint. MERGE is effectively MATCH or CREATE - and those two steps are run independently within the transaction. Given concurrent execution, and the "read committed" isolation level, there's a race condition between the two.
团队已经就如何在并发的情况下提供更高的保证进行了一些讨论,并将其作为一项功能要求予以考虑.
The team have done some discussion on how to provided a higher guarantee in the face of concurrency, and do have it noted as a feature request for consideration.
与此同时,他们向我保证,使用约束将提供您要查找的唯一性.
Meanwhile, they've assured me that using a constraint will provide the uniqueness you're looking for.
这篇关于Neo4j:MERGE创建重复的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!