当我对位置节点的地址有唯一约束但正在使用合并时,为什么我会遇到此 Cypher 语句的问题,该合并应该会发现它是否存在并且只返回该语句其余部分的 id .我错过了什么?
这是我的声明:
MERGE(l:Location{location_name:"Starbucks", address:"36350 Van Dyke Ave", city: "Sterling Heights",state: "MI", zip_code:"48312",type:"location",room_number:"",long:-83.028889,lat:42.561152})
CREATE(m:Meetup{meet_date:1455984000,access:"Private",status:"Active",type:"project",did_happen:"",topic:"New features for StudyUup",agenda:"This is a brainstorming session to come with with new ideas for the companion website, StudyUup. Using MatchUup as the base, what should be added, removed, or modified? Bring your thinking caps and ideas!"})
WITH m,l
MATCH (g:Project{title_slug:"studyuup"}) MATCH (p:Person{username:"wkolcz"})
WITH m,l,g,p
MERGE (g)-[:CREATED {rating:0}]->(m)
MERGE (m)-[:MEETUP_AT {rating:0}]->(l)-[:HOSTED_MEETUP]->(m)
MERGE (m)<-[:ATTENDING]-(p)
RETURN id(m) as meeting_id
我正进入(状态:
Node 416 already exists with label Location and property "address"=[36350 Van Dyke Ave]
最佳答案
您遇到了一个常见的误区MERGE
. MERGE
合并您在单个 MERGE
中指定的所有内容条款。所以操作顺序是:
:Location
具有您指定的所有属性的节点。 您的问题出现在第 3 步。因为具有您指定的所有属性的节点不存在,所以它转到第 3 步并尝试创建一个具有所有这些属性的节点。那就是违反了唯一性约束的时候。
最佳做法是合并您限制为唯一的属性,然后使用
SET
更新其他属性。在你的情况下:MERGE (l:Location {address:"36350 Van Dyke Ave"})
SET l.location_name = "Starbucks",
l.city = "Sterling Heights"
...
相同的逻辑将应用于您稍后在查询中合并的关系。如果整个模式不存在,它将尝试创建整个模式。这就是为什么您应该坚持以下最佳实践:
MERGE (node1:Label1 {unique_property: "value"})
MERGE (node2:Label2 {unique_property: "value"})
MERGE (node1)-[:REL]-(node2)
关于neo4j - Cypher 'Node Already Exists' 问题与 MERGE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35381968/