我想在同一张表中有父子关系。以下是我所拥有的,但它不起作用。通过“不工作”,我的意思是当我调用.children
某个Item
对象时,它给了我[]
. 我的代码中有什么错误以及如何正确执行?谢谢。
parents_children = Table('parents_children', Base.metadata,
Column('parent_id', bigint, ForeignKey('items.id', name='a'), primary_key=True),
Column('child_id', bigint, ForeignKey('items.id', name='b'), primary_key=True)
)
class Item(Base):
__tablename__ = 'items'
id = Column(bigint, primary_key=True, autoincrement=True)
name = Column(types.String(80), default="")
display_name = Column(types.String(80), default="")
version = Column(types.String(80), default="")
discriminator = Column(item_tag_enum, nullable=False)
children = relationship('Item',
secondary=parents_children,
primaryjoin=id == parents_children.c.parent_id,
secondaryjoin=id == parents_children.c.child_id,
backref='parents')
__mapper_args__ = {'polymorphic_on' : discriminator}
# Item subclasses omitted because that's irrelevant to the question.