MiniMe
MiniMe

Reputation: 1275

I am losing my mind with py2neo: node in the graph but not in the graph

So this is a graph that contains network devices Preexisting nodes were added and now I am trying to add more nodes and add relationships to the graph. What am I doing wrong. At the bottom of the second block of code the error message is saying the node is not in this graph but as you can see the node is listed as present

matcher=NodeMatcher(db)
nodes=matcher.match()
for node in nodes:
    print (node)
node1=matcher.match(name="mxxx103")
print (node1)
node2=matcher.match(name='mxxxcvss01')
print(node2)
for rel in db.relationships.match((node1,node2)):
    print (rel)

And the output when running the above code

 (_9787:Device {model: 'ASR1000', name: 'mxxx103', scanned: 'Yes'})
    (_9788:Device {model: 'ASR1000', name: 'lxxx100', scanned: 'Yes'})
    (_9789:Device {model: 'ASR1000', name: 'mxxx100', scanned: 'Yes'})
    (_9790:Device {model: 'ASR1000', name: 'txxx100', scanned: 'Yes'})
    (_9791:Device {model: 'ASR1000', name: 'mxxx101', scanned: 'Yes'})
    (_9792:Device {model: 'ASR1000', name: 'mxxx102', scanned: 'Yes'})
    (_9793:Device {model: 'ASR1000', name: 'txxx101', scanned: 'Yes'})
    (_9794:Device {model: 'ASR1000', name: 'lxxx101', scanned: 'Yes'})
    (_9795:Device {model: 'ASR1000', name: 'cxxx100', scanned: 'Yes'})
    (_9796:Device {model: 'ASR1000', name: 'cxxx101', scanned: 'Yes'})
    (_9797:Device {capabilities: 'R S I', model: 'WS-C4500X', name: 'mxxxcvss01'})
    <py2neo.matching.NodeMatch object at 0x02CCB870>
    <py2neo.matching.NodeMatch object at 0x02CCBCD0>
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-135-653e282922e7> in <module>
          7 node2=matcher.match(name='mxxxcvss01')
          8 print(node2)
    ----> 9 for rel in db.relationships.match((node1,node2)):
         10     print (rel)

    C:\Utils\WPy3.6 -32-Qt5\python-3.6.7\lib\site-packages\py2neo\matching.py in __iter__(self)
        266         """ Iterate through all matching relationships.
        267         """
    --> 268         query, parameters = self._query_and_parameters()
        269         for record in self.graph.run(query, parameters):
        270             yield record[0]

    C:\Utils\WPy3.6 -32-Qt5\python-3.6.7\lib\site-packages\py2neo\matching.py in _query_and_parameters(self, count)
        311             if len(self._nodes) >= 1 and self._nodes[0] is not None:
        312                 start_node = Node.cast(self._nodes[0])
    --> 313                 verify_node(start_node)
        314                 clauses.append("MATCH (a) WHERE id(a) = {x}")
        315                 parameters["x"] = start_node.identity

    C:\Utils\WPy3.6 -32-Qt5\python-3.6.7\lib\site-packages\py2neo\matching.py in verify_node(n)
        288         def verify_node(n):
        289             if n.graph != self.graph:
    --> 290                 raise ValueError("Node %r does not belong to this graph" % n)
        291             if n.identity is None:
        292                 raise ValueError("Node %r is not bound to a graph" % n)

    ValueError: Node ({model: 'ASR1000', name: 'mxxx103', scanned: 'Yes'}) does not belong to this graph

Upvotes: 0

Views: 808

Answers (1)

MiniMe
MiniMe

Reputation: 1275

OK I managed to find the mistake, it seems that I need to look again and again over the return of each method and the data types py2neo uses

The below code worked. My mistake was to believe that the node.match returns a node. That is not the case. The below code worked

matcher=NodeMatcher(db)
nodes=matcher.match()
for node in nodes:
    print (node)
node1=matcher.match(name="mdc103")
list (node1)
node2=matcher.match(name='mdccvss01')
list(node2)
type(node1)
node1 = db.evaluate('MATCH (x) WHERE x.name="mxxx103" RETURN(x)')
print(node1)
node2 = db.evaluate('MATCH (x) WHERE x.name="mxxxcvss01" RETURN(x)')
print(node2)
for rel in db.relationships.match((node1,node2)):
    print (rel)

Upvotes: 1

Related Questions