简体   繁体   English

如何限制 Neo4j 中的两个节点之间只有一种关系?

[英]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.

相关问题 如何在neo4j中找到两个节点之间的最短关系? - how to find the shortest relationship between the two nodes in neo4j? 如何在两个节点之间的neo4j中创建关系? - How do I create a relationship in neo4j between two nodes? 如何显示 neo4j 中的所有节点和两个节点之间的关系? - How can i display the all nodes and relationships between two nodes in neo4j? 在Neo4J中创建两个节点之间的关系,但如果其中一个节点不存在则在同一个调用中创建它 - In Neo4J create relationship between two nodes, but if one of nodes don't exists create it in the same call 我想在 Neo4j 中的两个已经存在的节点之间添加一个关系——如何使用 Spring Data Neo4jRepository 来做到这一点? - I want to add a Relationship between two already existing nodes in Neo4j - how to do that using Spring Data Neo4jRepository? 如何查询Neo4j节点及其之间的关系? - How to query Neo4j nodes and the relationship between the nodes? Neo4j Client在两个现有节点之间创建关系 - Neo4j Client Creating Relationship between two existing Nodes 在两个节点之间创建关系类型neo4j - Creating relationship type between two nodes neo4j Neo4j仅过滤一种关系的节点 - Neo4j filtering nodes with only one type of relationship Neo4j gem-仅在两个节点之间不存在时才创建关系 - Neo4j gem - Create relationship between two nodes only if it doesn't exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM