SQLAlchemy:单向关系,相关子查询

2024-06-07 09:47:25 发布

您现在位置:Python中文网/ 问答频道 /正文

提前谢谢你的帮助。在

我有两个实体,人类和黑猩猩。每个都有一个度量集合,其中可以包含MetricBlock的子类,例如completeboodcount(带有白细胞、红细胞、血小板)字段。在

所以我的对象模型看起来像(请原谅ASCII艺术):

---------   metrics    ---------------      ----------------------
| Human | ---------->  | MetricBlock | <|-- | CompleteBloodCount |
---------              ---------------      ----------------------
                        ^
---------    metrics    |
| Chimp | --------------
---------

通过下表实现:

^{pr2}$

所以人类知道它的度量块,但是度量块不知道它的人类或黑猩猩。在

我想用SQLAlchemy编写一个查询来查找特定人类的所有完整血量。在SQL中,我可以写如下内容:

SELECT cbc.id
FROM complete_blood_count cbc
WHERE EXISTS (
   SELECT 1
   FROM human h
       INNER JOIN human_to_metric_block h_to_m on h.id = h_to_m.human_id
   WHERE
       h_to_m.metric_block_id = cbc.id
)

我正在努力用SQLAlchemy来写这篇文章。我相信correlate()、any()或别名连接可能会有所帮助,但MetricBlock不知道它的人类或黑猩猩这一事实对我来说是个绊脚石。在

有人对如何编写这个查询有什么建议吗?另外,是否有其他策略可以更好地使用SQLAlchemy来定义模型?在

谢谢你的帮助。在

Python 2.6
SQLAlchemy 0.7.4
Oracle 11g

编辑:

HumanToMetricBlock定义为:

humanToMetricBlock = Table(
    "human_to_metric_block",
    metadata,
    Column("human_id", Integer, ForeignKey("human.id"),
    Column("metric_block_id", Integer, ForeginKey("metric_block.id")
)

per the manual。在


Tags: tofrom模型idsqlalchemy度量人类block
1条回答
网友
1楼 · 发布于 2024-06-07 09:47:25

每一种灵长类动物都应该有一个唯一的身份证,不管他们是什么类型的灵长类动物。如果不是这样的话,我肯定每个表都有一个单独的属性。在

因此,我将按照以下方式构建这个问题: 创建一个父对象灵长类动物并从中衍生出人类和黑猩猩。本例使用的是单表继承,但您可能希望基于它们的属性使用联接表继承。在

class Primate(Base):
    __tablename__ = 'primate'
    id = Column(Integer, primary_key=True)
    genus = Column(String)
    ...attributes all primates have...
    __mapper_args__ = {'polymorphic_on': genus, 'polymorphic_identity': 'primate'}

class Chimp(Primate):
    __mapper_args__ = {'polymorphic_identity': 'chimp'}
    ...attributes...

class Human(Primate):
    __mapper_args__ = {'polymorphic_identity': 'human'}
    ...attributes...

class MetricBlock(Base):
    id = ...

然后创建一个多对多表(可以改用关联代理):

^{pr2}$

然后,我将按如下方式构造查询(注意,不需要on子句,因为SQLAlchemy可以自动推断关系,因为没有歧义):

^{3}$

如果要按灵长类动物类型进行筛选,请连接灵长类表并进行筛选:

query = query.join(Primate, Primate.id == PrimateToMetricBlock.primate_id).\
    filter(Primate.genus == 'human')

否则,如果您知道灵长类动物的ID(primate_ID),则不需要额外的连接:

query = query.filter(PrimateToMetricBlock.primate_id == primate_id)

如果只检索一个对象,请使用以下命令结束查询:

return query.first()

否则:

return query.all()

像这样形成你的模型应该可以消除任何混乱,并使一切变得更简单。如果我遗漏了什么,告诉我。在

相关问题 更多 >

    热门问题