如何限制Neo4j中两个节点之间只有一种关系? [英] How can I limit to only one relationship between two nodes in Neo4j?
问题描述
我有以下图形:
当前,我正在使用此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 代替
Instead, you can use MERGE instead of CREATE
to avoid creating duplicate relationships.
例如,此查询将仅创建DEPENDENT_ON
关系(如果尚不存在);否则,将创建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中两个节点之间只有一种关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!