问题描述
我有以下图表:
目前我正在使用这个 QUERY 添加两个节点之间的关系:
Currently I am using this QUERY to add a relationship between two nodes:
MATCH (a:Service),(b:Service)
WHERE a.service_id = 'cs2322' and b.service_id = 'ab3232'
CREATE (a)-[r:DEPENDENT_ON]->(b)
RETURN type(r)
但是我不想在任何两个节点之间有多个关系,因为我想可视化我的服务以及它们之间的依赖关系,所以我不能让一个服务两次依赖另一个.
However I dont want to have more than one relationship between any two nodes, because I want to visualise my services and the dependency between them, so I cannot have a service being two times dependent on the other.
如果我尝试在两个节点之间创建关系,而这两个节点之间已经在每个方向上建立了关系,那么有什么方法可以限制它以强制 Neo4j 服务器抛出错误?
Is there any way I can limit this to force neo4j server to throw an error if I try to create a relationship between two nodes which already have a relationship per direction with one-another?
推荐答案
如果创建重复的关系,没有内置的方法可以抛出错误.但这也是执行此类政策的一种非常昂贵的方式.
There is no built-in way to throw an error if you create a duplicate relationship. But that would also be a pretty expensive way to enforce such a policy.
相反,您可以使用 MERGE 而不是 CREATE
以避免创建重复的关系.
Instead, you can use MERGE instead of CREATE
to avoid creating duplicate relationships.
例如,如果DEPENDENT_ON
关系不存在,这个查询只会创建它;否则,它只会将现有关系绑定到 r
:
For example, this query will only create the DEPENDENT_ON
relationship if it does not already exist; otherwise, it will just bind the existing relationship to r
:
MATCH (a:Service), (b:Service)
WHERE a.service_id = 'cs2322' AND b.service_id = 'ab3232'
MERGE (a)-[r:DEPENDENT_ON]->(b)
RETURN TYPE(r)
这篇关于如何限制 Neo4j 中两个节点之间只有一种关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!