[英]How can I limit to only one relationship between two nodes in Neo4j?
I have the following graph:我有以下图表:
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.但是,我不想在任何两个节点之间建立一个以上的关系,因为我想可视化我的服务以及它们之间的依赖关系,所以我不能让一个服务两次依赖另一个。
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?如果我尝试在两个节点之间创建关系,这两个节点已经在每个方向上相互建立关系,我有什么办法可以限制它以强制 neo4j 服务器抛出错误?
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.但这也是执行此类政策的一种非常昂贵的方式。
Instead, you can use MERGE instead of CREATE
to avoid creating duplicate relationships.相反,您可以使用MERGE而不是CREATE
来避免创建重复的关系。
For example, this query will only create the DEPENDENT_ON
relationship if it does not already exist;例如,这个查询只会在DEPENDENT_ON
关系不存在的情况下创建它; otherwise, it will just bind the existing relationship to r
:否则,它只会将现有关系绑定到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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.