SqlAlchemy 1.0.0添加' AND NULL'加入没有定义外键的地方

时间:2015-07-29 07:56:47

标签: python mysql orm sqlalchemy

我在mySpql服务器上使用sqlalchemy版本1.0.0在python中构建查询。 我们没有在模式中定义外键。

我想构建下一个查询:

select s.visit_date, ss.store_number_1, count(p.pk) num_of_probes
from probedata.session s
inner join probedata.scene ps
on s.session_uid = ps.session_uid
inner join probedata.probe p
on ps.pk = p.scene_fk
inner join static.stores ss
on s.store_fk = ss.pk
where s.status = 'Completed'
group by s.visit_date, ss.store_number_1

查询的代码是:

 num_of_probes = session.query(StoreSession.visit_date, Stores.store_number_1, func.count(Probe.pk).label('num_of_probes')) \
            .select_from(StoreSession) \
            .join(Scene, StoreSession.session_uid == Scene.session_uid) \
            .join(Probe, Scene.pk ==Probe.scene_fk) \
            .join(Stores, StoreSession.store_fk == Stores.pk) \
            .filter(StoreSession.status == 'Completed') \
            .group_by(StoreSession.visit_date, Stores.store_number_1)

我收到的查询是:

SELECT probedata.session.visit_date AS probedata_session_visit_date, static.stores.store_number_1 AS static_stores_store_number_1, count(probedata.probe.pk) AS num_of_probes 
FROM probedata.session 
JOIN probedata.scene 
ON probedata.session.session_uid = probedata.scene.session_uid AND NULL 
JOIN probedata.probe ON probedata.scene.pk = probedata.probe.scene_fk AND NULL 
JOIN static.stores ON probedata.session.store_fk = static.stores.pk AND NULL 
WHERE probedata.session.status = :status_1 
GROUP BY probedata.session.visit_date, static.stores.store_number_1

问题是sqlachemy连接添加" AND NULL"到sql查询中的每个连接。

我知道它确实没有定义外键,并且SqlAlcemy 0.9.8没有添加它。

如何解除' AND NULL'条件在1.0.0?

谢谢, 可人

1 个答案:

答案 0 :(得分:1)

有关该问题的相关信息: Append AND NULL condition on joins