Search code examples
pythonneo4jpy2neo

Update property relation with py2neo neo4j


I'm trying to check if a relation between two nodes exists but i get this error:

raise TypeError("Nodes for relationship match end points must be bound") TypeError: Nodes for relationship match end points must be bound

below my code:

graph = Graph(user='neo4j')

src = Node(src_type, internal_id=int(src_id))
dst = Node(dst_type, internal_id=int(dst_id))

src_voted_dst = Relationship(src, "VOTED", dst)

for elem in graph.match(start_node=src, rel_type="VOTED", end_node=dst, bidirectional=True):
    elem.properties["vote"] = elem.properties["vote"] + 1
    elem.push()
    break
else:
    src_voted_dst.properties["vote"] = 1
    graph.merge(src_voted_dst)

Solution

  • In the code:

    src = Node(src_type, internal_id=int(src_id))
    dst = Node(dst_type, internal_id=int(dst_id))
    

    src and dst are created locally, but not bound to these nodes in the database. Bind the local nodes to the database with merge:

    db_src = graph.merge(src)
    db_dst = graph.merge(dst)
    

    Then match should then work:

    for elem in graph.match(db_src, "VOTED", db_dst)
    

    (note that elem.properties["vote"] will not work, there should be something link elem.start_node()["vote"] or elem.end_node()["vote"])