0

我尝试在 SQLAlchemy (ORM) 中执行以下请求:

SELECT id, ref_prog FROM stepvand_1c_1t.equipment_day_hour WHERE id IN (SELECT id FROM stepvand_1c_1t.equipment WHERE equipment_type='L')

我做了:

subq = session.query(Equipment)
subq = subq.filter(Equipment.equipment_type == "L").subquery()

query = session.query(EquipmentDayHour)
query = query.filter(EquipmentDayHour.id.in_(subq))

但这不起作用...

Python 告诉我子请求的列太多。

4

1 回答 1

1

I think you should only change one line of your sample code:

# error: includes all columns of Equipment
`subq = session.query(Equipment)`
# correct: include only ID column
`subq = session.query(Equipment.id)`

However, I believe that you can do this without subquery:

query = (session.query(EquipmentDayHour).
         # version-1: if you have a relationship between EquipmentDayHour and Equipment
         join(Equipment).
         # version-2: if you do not have such relationship
         #join(Equipment, EquipmentDayHour.id==Equipment.id).
         filter(Equipment.equipment_type == "L")
        )
于 2012-06-26T13:08:12.520 回答