简体   繁体   English

SQLAlchemy 在一个关系中组合多个子类

[英]SQLAlchemy Combine Multiple Child Classes in one Relationship

I am wondering if somebody can help me out on my SQLAlchemy problem of combining multiple child classes in one relationship.我想知道是否有人可以帮助我解决在一个关系中组合多个子类的 SQLAlchemy 问题。

I am working on a project management app where I have a task breakdown structure as follows:我正在开发一个项目管理应用程序,其中的任务分解结构如下:

Summary Task -> Task -> Subtask -> Located Task摘要任务 -> 任务 -> 子任务 -> 定位任务

Summary Tasks have the ability to reference another summary task forming a hierarchy.摘要任务能够引用另一个形成层次结构的摘要任务。 Meanwhile, tasks does not necessarily belong to a summary task.同时,任务不一定属于摘要任务。

My current solution is to reference them in separate relationships:我目前的解决方案是在单独的关系中引用它们:

class SummaryTask(Base):
    __tablename__ = 'summary_task'

    id = Column(Integer, primary_key=True)
    schedule_id = Column(ForeignKey('schedule.id'), primary_key=True)
    parent_id = Column(Integer)
    parent_schedule_id = Column(Integer)
    additional_data = Column(Unicode(50))

    children = relationship('SummaryTask', backref=backref('parent', remote_side=[id, schedule_id]))
    tasks = relationship('Task', backref=backref('parent'))

class Task(Base):
    __tablename__ = 'task'
    __table_args__ = (ForeignKeyConstraint(['summary_task_id', 'schedule_id'], ['summary_task.id', 'summary_task.schedule_id']))

    id = Column(Integer, primary_key=True)
    schedule_id = Column(ForeignKey('schedule.id'), primary_key=True)
    summary_task_id = Column(Integer)
    
    summary_task = relationship('SumTask')

The code snippet above works for me, but I would like to combine summary tasks (children) and tasks in children, as both are indeed children of summary tasks.上面的代码片段对我有用,但我想将摘要任务(子项)和子项中的任务结合起来,因为两者确实是摘要任务的子项。

class SummaryTask(Base):
    ...

    children = relationship('SummaryTask', 'Task' ...)

My interface will then be as follows from Pydantic:我的界面将如下来自 Pydantic:

class SummaryTaskInterface(BaseModel):
    id: int
    schedule_id: int
    parent_id: Optional[int] = None
    parent_schedule_id: Optional[int] = None
    additional_data: str

    children: List[Union['SummaryTaskInterface', 'TaskInterface']] = []

I hope someone can help my out here.我希望有人能在这里帮助我。

Kind regards亲切的问候

I ended up with this solution.我最终得到了这个解决方案。

class SummaryTask(Base):
    __tablename__ = 'summary_task'

    id = Column(Integer, primary_key=True)
    schedule_id = Column(ForeignKey('schedule.id'), primary_key=True)
    parent_id = Column(Integer)
    parent_schedule_id = Column(Integer)
    additional_data = Column(Unicode(50))

    summary_tasks = relationship('SummaryTask', backref=backref('parent', remote_side=[id, schedule_id]))
    tasks = relationship('Task', backref=backref('parent'))

    @hybrid_property
    def children(self):
        return self.summary_tasks + self.tasks

With the already suggested interface.使用已经建议的界面。

class SummaryTaskInterface(BaseModel):
    id: int
    schedule_id: int
    parent_id: Optional[int] = None
    parent_schedule_id: Optional[int] = None
    additional_data: str

    children: List[Union['SummaryTaskInterface', 'TaskInterface']] = []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM