我正在努力应对以下映射。 我的意图是拥有一个myobject-class,其中包含父对象列表(相同类型)和子对象列表(也是对象类型)。 经过大量搜索,我找到了用于多对多或自引用的示例,但没有一个答案符合我的需求。 这是我现在拥有的:
class ObjectDependency():
Id = Column("OBJ_DEPENDENCY_ID", BigInteger(), primary_key = True)
_childobjectid = Column("CHILD_OBJECT_ID"
, BigInteger()
, ForeignKey(MyObject.Id)
)
ChildObject = relationship(MyObject, foreign_keys = [_childobjectid])
_parentobjectid = Column("PARENT_OBJECT_ID"
, BigInteger()
, ForeignKey(MyObject.Id)
)
ParentObject = relationship(MyObject, foreign_keys = [_parentobjectid])
class MyObject():
Id = Column("OBJECT_ID", BigInteger(), primary_key = True)
#..other columns
ParentObjectDependencies = relationship("ObjectDependency"
, back_populates = "ParentObject"
, foreign_keys = "ObjectDependency._childobjectid"
)
ChildObjectDependencies = relationship("ObjectDependency"
, back_populates = "ChildObject"
, foreign_keys = "ObjectDependency._parentobjectid"
)
ParentDependencyObjects = association_proxy("ParentObjectDependencies", "ParentObject")
ChildDependencyObjects = association_proxy("ChildObjectDependencies", "ChildObject")
如果我做这样的事情,它也不会抛出错误:
obj1.ChildObjectDependencies.append(obj2)
它还将我的关联对象写入数据库,但是这些值没有意义。在数据库中,obj1通过我的Dependency对象引用obj2。 我如何使我的关系正常运转?