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

1 投票
1 回答
2939 浏览
提问于 2025-04-17 08:58

提前感谢你的帮助。

我有两个实体,一个是人(Human),另一个是猩猩(Chimp)。每个实体都有一组指标,这些指标可以包含一个叫做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写一个查询,找出某个人的所有完整血细胞计数。在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")
)

根据手册

1 个回答

3

每种灵长类动物都应该有一个独特的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)

如果你想按灵长类动物类型进行筛选,可以连接Primate表并进行过滤:

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

这样构建你的模型应该能消除任何困惑,实际上会让一切变得更简单。如果我漏掉了什么,请告诉我。

撰写回答