Хотя я пока не знаю, как реализовать «братьев и сестер», вот как можно иметь много самореферентных отношений «многие ко многим»:
Connection = Table('connection',
Column('child_id', String, ForeignKey('node.id')),
Column('parent_id', String, ForeignKey('node.id')),
UniqueConstraint('parent_id', 'child_id', name='unique_usage')
)
class Node(Model):
id = Column(String, primary_key=True)
name = Column(String)
# this is the result list of type Node
# where the current node is the "other party" or "child"
parents = relationship('Node', secondary=Connection,
primaryjoin=id == Connection.c.parent_id,
secondaryjoin=id == Connection.c.child_id)
# this is the result list of type Node
# where the current node is the "parent"
children = relationship('Node', secondary=Connection,
primaryjoin=id == Connection.c.child_id,
secondaryjoin=id == Connection.c.parent_id)
в основном, для каждого желаемого «многие к»-много отношения, создайте таблицу, представляющую отношение, затем добавьте отношение к вашему модулю.Вы можете иметь двусторонние отношения для каждого из них
Я отредактирую свой ответ позже, когда пойму, как сделать братьев и сестер