我目前正在一个项目中,我有一个与不同标签的节点具有相同关系类型的节点。
我有以下一组节点标签和关系:
节点标签:人,建筑物,汽车
关系:
(:人)-[:OWNS]->(:汽车)
(:Person)-[:OWNS]->(:Building)
(:建筑物)-[:持有]->(:汽车)
我遇到的问题是,当我尝试从人员对象调用建筑物对象时,我正在收回建筑物和汽车。
我在下面包含了我的代码。
import py2neo.ogm as pn
class Person(pn.GraphObject):
__primarykey__ = 'Id'
Id = pn.Property()
Name = pn.Property()
cars = pn.RelatedTo("Car", relationship_type="OWNS")
building = pn.RelatedTo("Building", relationship_type="OWNS")
def __init__(self, Id, name):
self.Id = Id
self.Name = name
class Building(pn.GraphObject):
__primarykey__ = 'Id'
Id = pn.Property()
Name = pn.Property()
company = pn.RelatedFrom(related_class=Person, relationship_type="OWNS")
cars = pn.RelatedTo(related_class="Car", relationship_type="HOLDS")
def __init__(self, Id, name):
self.Id = Id
self.Name = name
class Car(pn.GraphObject):
__primarykey__ = 'Id'
Id = pn.Property()
Make = pn.Property()
Model = pn.Property()
company = pn.RelatedFrom(related_class=Person, relationship_type="OWNS")
building = pn.RelatedFrom(related_class=Building, relationship_type="HOLDS")
def __init__(self, Id, make, model):
self.Id = Id
self.Make = make
self.Model = model
我遇到的问题是只想从“人”节点中调用相关建筑物。
from Nodes import *
import uuid
Me = Person(Id=str(uuid.uuid4()), name="Me")
OnlyBuilding = Building(Id=str(uuid.uuid4()), name="OnlyBuilding")
LandRover = Car(Id=str(uuid.uuid4()), make="Land Rover", model="Discovery")
Ford = Car(Id=str(uuid.uuid4()), make="Ford", model="Fiesta")
Me.building.add(OnlyBuilding)
Me.cars.add(LandRover)
for item in Me.building:
print(item)
这导致
<Building Id='uuid string 1'>
<Car Id='uuid string 2'>
我认为这是由于两者都通过“ [:OWNS]”与“人”相关。
除了更改关系类型以使每个人都不同之外,还有什么解决办法吗?