繁体   English   中英

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

[英]SQLAlchemy: One-Way Relationship, Correlated Subquery

在此先感谢您的帮助。

我有两个实体,人类和黑猩猩。 每个都有一个度量标准集合,这些度量标准可以包含MetricBlock的子类,例如CompleteBloodCount(带有字段WHITE_CELLS,RED_CELLS,PLATELETS)。

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

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

这是通过下表实现的:

Chimp (id, …)
Human (id, …)

MetricBlock (id, dtype)
CompleteBloodCount (id, white_cells, red_cells, platelets)
CholesterolCount (id, hdl, ldl)

ChimpToMetricBlock(chimp_id, metric_block_id)
HumanToMetricBlock(human_id, metric_block_id)

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

我想在SQLAlchemy中编写一个查询来查找特定人类的所有CompleteBloodCounts。 在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中写这个。 我相信related(),any()或别名联接可能会有所帮助,但是MetricBlock不知道其Human或Chimp的事实对我来说是一个绊脚石。

有没有人对如何编写此查询有任何建议? 另外,还有其他策略可以更好地与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")
)

根据手册

每个灵长类动物都应该有一个唯一的ID,无论它们是什么类型的灵长类动物。 我不确定为什么每组属性(MB,CBC,CC)都是单独的表,但是我假设它们具有多个维度(灵长类),例如时间,否则我将只有一个巨型表。

因此,我将以以下方式构造此问题:创建父对象灵长类动物并从中派生人类和黑猩猩。 此示例使用单表继承,但您可能希望根据其属性使用联接表继承。

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 = ...

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

class PrimateToMetricBlock(Base):
    id = Column(Integer, primary_key=True) # primary key is needed!
    primate_id = Column(Integer, ForeignKey('primate.id'))
    primate = relationship('Primate') # If you care for relationships. 
    metricblock_id = Column(Integer, ForeignKey('metric_block.id')
    metricblock = relationship('MetricBlock')

然后我会像这样构造查询(注意,onAl子句不是必需的,因为SQLAlchemy可以自动推断关系,因为没有歧义):

query = DBSession.query(CompleteBloodCount).\
    join(PrimateToMetricBlock, PrimateToMetricBlock.metricblock_id == MetricBlock.id)

如果要按灵长类动物类型过滤,请加入灵长类动物表并过滤:

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()

像这样形成你的模型应该消除任何混乱,并实际上使一切变得更简单。 如果我缺少什么,请告诉我。

暂无
暂无

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

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