Neo4J - 合并语句不创建具有关系的新节点 [英] Neo4J - Merge statement not creating new nodes with a relationship

查看:59
本文介绍了Neo4J - 合并语句不创建具有关系的新节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个查询,如果存在则构建 2 个新节点,然后它只是更新属性并添加它们之间的关系.

当我第一次创建节点和关系时,一切都很顺利.

For the first time when I'm creating the nodes and relationship everything is going fine.

// This is the first run

MERGE (Kunal:PERSON)
ON CREATE SET
    Kunal.name = 'Kunal',
    Kunal.type = 'Person',
    Kunal.created = timestamp()
ON MATCH SET
    Kunal.lastUpdated = timestamp()
MERGE (Bangalore: LOC)
ON CREATE SET
    Bangalore.name = 'Bangalore',
    Bangalore.type = 'Location',
    Bangalore.created = timestamp()
ON MATCH SET
    Bangalore.lastUpdated = timestamp()
MERGE (Kunal)-[r1:LIVES_IN]->(Bangalore)
RETURN * 

这里我要添加一个住在(关系)班加罗尔(节点)的节点Kunal(节点).第一次一切都很好.

Here I'm adding a node Kunal (node) who lives in (relation) Bangalore (node). Everything is fine for the first time.

下次我按如下方式添加不同的节点时:

The next time I'm adding a different node as follows:

// Next time

MERGE (John:PERSON)
ON CREATE SET
    John.name = 'John',
    John.type = 'Person',
    John.created = timestamp()
ON MATCH SET
    John.lastUpdated = timestamp()
MERGE (Bangalore: LOC)
ON CREATE SET
    Bangalore.name = 'Bangalore',
    Bangalore.type = 'Location',
    Bangalore.created = timestamp()
ON MATCH SET
    Bangalore.lastUpdated = timestamp()
MERGE (John)-[r1:LIVES_IN]->(Bangalore)
RETURN *

我要添加一个节点John,他住在(rel)班加罗尔(节点).

I am adding a node John who lives in (rel) Bangalore (node).

但这里的问题是,它再次从之前的合并语句中获取相同的节点值.

有人能解释一下吗?

还有什么解决方案,假设我们是否使用 Python 驱动程序在循环内运行上述合并查询.

Also what is the solution for this, if let's say if we are running the above merge queries inside of a loop using the Python Driver.

在此方面找不到任何内容

推荐答案

原因:

原因在于您的第一个和第二个 MERGE 语句的匹配模式.因为它们只测试Label PersonLoc 是否存在,而不是具有名称的具体节点PersonKunal.

Reason:

The reason lies in your matching pattern of the first and second MERGE statement. Because they only test for the existence of Label Person and Loc, but not a concrete node Person with the name Kunal.

正如@logisima 已经在此答案的评论中解释的那样:

As @logisima already explained in the comments of this answer:

您应该始终在主键上使用 MERGE(带有索引/唯一对其的约束)".

"You should always use a MERGE on a primary key (with an index / unique constraint on it)".

解决方案:

例如,您应该通过像 {name: 'your content'}WHERE id(kunal)=1234 之类的过滤来匹配具体的唯一节点.

Solution:

You should match a concrete, unique node by filtering like {name: 'your content'} or WHERE id(kunal)=1234 for instance.

这篇关于Neo4J - 合并语句不创建具有关系的新节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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