使用py2n删除特定关系

2024-05-06 08:17:13 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我们有两个节点,User和Post。关系,比如upvote,可以存在为(:User)-[upvote]->;(:Post)

如果以下查询结果为非空列表,则我希望删除该关系:

        rel_list = list(graph.match(start_node=user, end_node=post, rel_type="UPVOTED")
        if len(rel_list) > 0:
            # delete the relationship from the graph
        else:
            # create the relationship
            rel = Relationship(user, "UPVOTED", post)
            graph.create_unique(rel)

Tags: thenode节点关系createpostlistgraph
2条回答

要删除关系,应该使用separate方法:http://py2neo.org/v3/database.html#py2neo.database.Transaction.separate。在

您可能需要在GitHub中查看这个问题,以便对此进行一些讨论:https://github.com/nigelsmall/py2neo/issues/508

谨致问候

回答我自己的问题,希望这对其他人也有帮助。在

    rel_list = list(graph.match(start_node=user, end_node=post, rel_type="UPVOTED")
    if len(rel_list) > 0:
        # delete the relationship from the graph
        # fetch the ID of the relationship -> rel_id
        query = '''
        MATCH (a:NeoZotr)-[r:LIKED]->(b:NeoZot)
        WHERE ID(r) = {x}
        DELETE r
        '''
        graph.cypher.execute(query,x=rel_id)
    else:
        # create the relationship
        rel = Relationship(user, "UPVOTED", post)
        graph.create_unique(rel)

如果最后出现TypeError(就像在我的例子中一样),那么您获取的rel\uid可能是unicode样式。在这种情况下传递int(rel_id)。在

相关问题 更多 >