python - SQLAlchemy:单向关系、相关子查询

标签 python sqlalchemy

预先感谢您的帮助。

我有两个实体,人类和黑猩猩。每个都有一个指标集合,其中可以包含 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)

因此,人类知道其公制 block ,但公制 block 不知道其人类或黑猩猩。

我想在 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 中编写此内容。我相信 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 .

最佳答案

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

因此,我将按以下方式构建这个问题: 创建一个父对象 Primate 并从中派生出人类和黑猩猩。此示例使用单表继承,但您可能希望根据其属性使用连接表继承。

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

然后我将像这样构造查询(请注意,on 子句不是必需的,因为 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()

像这样形成你的模型应该可以消除任何困惑,并且实际上使一切变得更简单。如果我遗漏了什么,请告诉我。

关于python - SQLAlchemy:单向关系、相关子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8627567/

相关文章:

url_for 中的 Python flask 数组查询字符串

python - Google Cloud Endpoints 的自定义身份验证(而不是 OAuth2)

python - SQLAlchemy 核心 : order by desc

python - 当没有这样的命名列时,SQLite 给出找不到列的错误

python - 创建规则间隔的数字序列

python - opencv模式匹配不起作用

python - python 中导入错误

python - 使用sqlalchemy解析python3中的hstore

python - Flask SQLAlchemy order_by 关系

python - 如何在 Python/SQLAlchemy/Flask 中计算累积移动平均值