节点已添加到neo4j DB,但打印为“无”

2024-09-25 00:31:40 发布

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

我有以下代码:

import py2neo
from py2neo import Graph, Node, Relationship

def createRelationshipWithProperties():
    print("Start - Creating Relationships")
    # Authenticate the user using py2neo.authentication
    # Ensure that you change the password 'sumit' as per your database configuration.
    py2neo.authenticate("localhost:7474", "neo4j", "")
    # Connect to Graph and get the instance of Graph
    graph = Graph("http://localhost:7474/db/data/")
    # Create Node with Properties
    amy = Node("FEMALE", name="Amy")
    # Create one more Node with Properties
    kristine = Node("FEMALE",name="Kristine")
    # Create one more Node with Properties
    sheryl = Node("FEMALE",name="Sheryl")

    kristine_amy = Relationship(kristine,"FRIEND",amy,since=2005)
    print (kristine_amy)
    amy_sheryl = Relationship(sheryl,("FRIEND"),amy,since=2001)

    #Finally use graph Object and Create Nodes and Relationship
    #When we create Relationship between, then Nodes are also created. 
    resultNodes = graph.create(kristine_amy)
    resultNodes1 = graph.create(amy_sheryl)
    #Print the results (relationships)
    print("Relationship Created - ",resultNodes)
    print("Relationship Created - ",resultNodes1)

if __name__ == '__main__':
    createRelationshipWithProperties()

resultsNodes = graph.create行似乎将节点和关系提交给服务器,因为我在match(n) Return n时可以看到它们。但是,当代码打印resultsNodes时,我得到None,就好像它们不存在一样。这是我得到的结果:

Start - Creating Relationships
(kristine)-[:FRIEND {since:2005}]->(amy)
Relationship Created -  None
Relationship Created -  None

Tags: andthenamenodecreategraphprintcreated
1条回答
网友
1楼 · 发布于 2024-09-25 00:31:40

你使用的API不正确。create方法不返回节点,而是更新提供的参数。因此,要获取关系节点,您需要在执行create之后询问relationship对象。你知道吗

相关问题 更多 >