(py2neo)如何检查是否存在关系?

V_T*_*_TS 3 python neo4j py2neo

假设我有以下代码:

    link = neo4j.Path(this_node,"friends",friend_node) #create a link between 2 nodes
    link.create(graph_db) #add the link aka the 'path' to the database
Run Code Online (Sandbox Code Playgroud)

但是我们稍后会说:

link2 = neo4j.Path(friend_node,"friends",this_node)
link2.create_or_fail(graph_db)
Run Code Online (Sandbox Code Playgroud)

基本上,link.create_or_fail()将是一个将link2路径添加到数据库的函数,或者如果路径已经存在则失败.

在这种情况下,当我打电话时link = neo4j.Path(this_node,"friends",friend_node),我已经创建了一条路径this_node,friend_node因此link2.create_or_fail(graph_db)不应该做任何事情.这样的功能可能吗?

Sal*_*ina 5

我为此做的是使用以下功能:

def create_or_fail(graph_db, start_node, end_node, relationship):
    if len(list(graph_db.match(start_node=start_node, end_node=end_node, rel_type=relationship))) > 0:
        print "Relationship already exists"
        return None
    return graph_db.create((start_node, relationship, end_node))
Run Code Online (Sandbox Code Playgroud)

该方法graph_db.match()查找与给定过滤器的关系.

以下链接帮助我了解这一点.