Cypher“节点已经存在"问题与 MERGE [英] Cypher 'Node Already Exists' issue with MERGE

查看:31
本文介绍了Cypher“节点已经存在"问题与 MERGE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我对位置节点的地址有唯一约束但正在使用合并时,为什么我会遇到这个 Cypher 语句的问题,但我正在使用合并,该合并应该发现它是否存在并且只返回其余的 id的声明.我错过了什么?

I am preplexed on why I am getting an issue with this Cypher statment when I have a unique constraint on the address of the location node but am using a merge which should find that if it exists and only return the id for the rest of the statment. What am I missing?

这是我的声明:

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 子句中指定的所有内容.所以操作顺序是:

You've encountered a common misunderstanding of MERGE. MERGE merges on everything you've specified within the single MERGE clause. So the order of operations are:

  1. 使用您指定的所有属性搜索:Location节点.
  2. 如果找到,返回节点.
  3. 如果未找到,请创建节点.

您的问题出现在第 3 步.因为具有您指定的所有属性的节点不存在,所以它转到第 3 步并尝试创建一个具有所有这些属性的节点.那就是违反了唯一性约束的时候.

Your problem occurs at step 3. Because a node with all of the properties you've specified does not exist, it goes to step 3 and tries to create a node with all of those properties. That's when your uniqueness constraint is violated.

最佳做法是合并您限制为唯一的属性,然后使用 SET 更新其他属性.在你的情况下:

The best practice is to merge on the property that you've constrained to be unique and then use SET to update the other properties. In your case:

MERGE (l:Location {address:"36350 Van Dyke Ave"})
SET l.location_name = "Starbucks",
     l.city = "Sterling Heights"
...

同样的逻辑将应用于您稍后在查询中合并的关系.如果整个模式不存在,它将尝试创建整个模式.这就是为什么您应该坚持以下最佳实践:

The same logic is going to apply for the relationships you're merging later in the query. If the entire pattern doesn't exist, it's going to try to create the entire pattern. That's why you should stick to the best practice of:

MERGE (node1:Label1 {unique_property: "value"})
MERGE (node2:Label2 {unique_property: "value"})
MERGE (node1)-[:REL]-(node2)

这篇关于Cypher“节点已经存在"问题与 MERGE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆