Wie wähle ich eine rekursive Entität in SQLALCHEMY ausPython

Python-Programme
Anonymous
 Wie wähle ich eine rekursive Entität in SQLALCHEMY aus

Post by Anonymous »

Ich speichere ein Diagramm in einer Datenbank. Es hat eine Struktur wie: < /p>

Code: Select all

class SuperNode(Base):
__tablename__ = "supernodes"

id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)

nodes = relationship("Node", back_populates="supernode")

class Node(Base):
__tablename__ = "nodes"

id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
supernode_id = Column(Integer, ForeignKey("supernodes.id"), nullable=False)
sub_node_id = Column(Integer, ForeignKey("supernodes.id"), nullable=True)

supernode = relationship("SuperNode", back_populates="nodes")
sub_node = relationship("SuperNode", foreign_keys=[sub_node_id])

children = relationship(
"Node",
secondary=association_table,
primaryjoin=id == association_table.c.parent_id,
secondaryjoin=id == association_table.c.child_id,
backref="parents"
)

association_table = Table(
"parent_child_association",
Base.metadata,
Column("parent_id", Integer, ForeignKey("nodes.id"), primary_key=True),
Column("child_id", Integer, ForeignKey("nodes.id"), primary_key=True),
)

fyi: Ich verwende Sqlalchemy 2.0 mit aiosqlite
Nun, ich habe Probleme, eine Anweisung zu erstellen, die durch ID ein Supernode wird, und dann die gesamte Hierarchie der Knoten ihre sub_node

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post