ObjectModel 访问传入关系
ObjectModel accessing incoming relations
我有两个节点 A
和 B
。它们具有从 A
到 B
的定向关系。
因此,A
具有类型为 RelatedTo
的 ConnectedTo
属性。但是,我想遍历所有 B
节点并访问来自 A
.
的传入关系
我该怎么做?
我尝试将 RelatedFrom
类型的 ConnectedTo
属性添加到 B
,但是在查询图表时我得到 ValueError('Invalid Identifier')
.
class A(GraphObject):
__primarykey__ = "hash"
hash = Property()
ConnectedTo = RelatedTo('B')
def __init__(self, hash):
self.hash = hash
class B(GraphObject):
__primarykey__ = "hash"
hash = Property()
ConnectedTo = RelatedFrom('A')
def __init__(self, hash):
self.hash = hash
>>> a = A("testA")
>>> b = B("testB")
>>> a.ConnectedTo.add(b)
>>> graph.push(a)
>>> graph.push(b)
>>> test = B.select(graph).first()
错误结果:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/py2neo/ogm.py", line 442, in first
return self._object_class.wrap(super(GraphObjectSelection, self).first())
File "/usr/local/lib/python2.7/dist-packages/py2neo/ogm.py", line 344, in wrap
_ = getattr(inst, attr)
File "/usr/local/lib/python2.7/dist-packages/py2neo/ogm.py", line 90, in __get__
related[key] = RelatedObjects(cog.node, self.direction, self.relationship_type, self.related_class)
File "/usr/local/lib/python2.7/dist-packages/py2neo/ogm.py", line 135, in __init__
self.__relationship_pattern = "(a)<-[_:%s]-(b)" % cypher_escape(relationship_type)
File "/usr/local/lib/python2.7/dist-packages/py2neo/database/cypher.py", line 221, in cypher_escape
writer.write_identifier(identifier)
File "/usr/local/lib/python2.7/dist-packages/py2neo/database/cypher.py", line 78, in write_identifier
raise ValueError("Invalid identifier")
ValueError: Invalid identifier
解决方案比预期的要简单:
class TestA(GraphObject):
__primarykey__ = "hash"
hash = Property()
CONNECTEDTO = RelatedTo('TestB')
def __init__(self, hash):
self.hash = hash
class TestB(GraphObject):
__primarykey__ = "hash"
hash = Property()
CONNECTEDTO = RelatedFrom('TestA', "CONNECTEDTO")
def __init__(self, hash):
self.hash = hash
>>> a = A("testA")
>>> b = B("testB")
>>> a.ConnectedTo.add(b)
>>> graph.push(a)
>>> graph.push(b)
>>> test = B.select(graph).first()
>>> list(test.CONNECTEDTO)
[ TestA ]
重要的部分是RelatedFrom('TestA','CONNECTEDTO')
。您必须指定传入连接的名称。
我有两个节点 A
和 B
。它们具有从 A
到 B
的定向关系。
因此,A
具有类型为 RelatedTo
的 ConnectedTo
属性。但是,我想遍历所有 B
节点并访问来自 A
.
我该怎么做?
我尝试将 RelatedFrom
类型的 ConnectedTo
属性添加到 B
,但是在查询图表时我得到 ValueError('Invalid Identifier')
.
class A(GraphObject):
__primarykey__ = "hash"
hash = Property()
ConnectedTo = RelatedTo('B')
def __init__(self, hash):
self.hash = hash
class B(GraphObject):
__primarykey__ = "hash"
hash = Property()
ConnectedTo = RelatedFrom('A')
def __init__(self, hash):
self.hash = hash
>>> a = A("testA")
>>> b = B("testB")
>>> a.ConnectedTo.add(b)
>>> graph.push(a)
>>> graph.push(b)
>>> test = B.select(graph).first()
错误结果:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/py2neo/ogm.py", line 442, in first
return self._object_class.wrap(super(GraphObjectSelection, self).first())
File "/usr/local/lib/python2.7/dist-packages/py2neo/ogm.py", line 344, in wrap
_ = getattr(inst, attr)
File "/usr/local/lib/python2.7/dist-packages/py2neo/ogm.py", line 90, in __get__
related[key] = RelatedObjects(cog.node, self.direction, self.relationship_type, self.related_class)
File "/usr/local/lib/python2.7/dist-packages/py2neo/ogm.py", line 135, in __init__
self.__relationship_pattern = "(a)<-[_:%s]-(b)" % cypher_escape(relationship_type)
File "/usr/local/lib/python2.7/dist-packages/py2neo/database/cypher.py", line 221, in cypher_escape
writer.write_identifier(identifier)
File "/usr/local/lib/python2.7/dist-packages/py2neo/database/cypher.py", line 78, in write_identifier
raise ValueError("Invalid identifier")
ValueError: Invalid identifier
解决方案比预期的要简单:
class TestA(GraphObject):
__primarykey__ = "hash"
hash = Property()
CONNECTEDTO = RelatedTo('TestB')
def __init__(self, hash):
self.hash = hash
class TestB(GraphObject):
__primarykey__ = "hash"
hash = Property()
CONNECTEDTO = RelatedFrom('TestA', "CONNECTEDTO")
def __init__(self, hash):
self.hash = hash
>>> a = A("testA")
>>> b = B("testB")
>>> a.ConnectedTo.add(b)
>>> graph.push(a)
>>> graph.push(b)
>>> test = B.select(graph).first()
>>> list(test.CONNECTEDTO)
[ TestA ]
重要的部分是RelatedFrom('TestA','CONNECTEDTO')
。您必须指定传入连接的名称。