SQLAlchemy and empty IN clause

前端 未结 5 570
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 10:17

I found out that SQLAlchemy translates

db.query(...).filter(A.id.in_(ids))

into

SELECT ...
FROM a
WHERE a.id != a.id

5条回答
  •  日久生厌
    2021-02-02 10:38

    use subquery, if ids is empty, will (never executed).

    example:

    subquery = db.query(SomeTable.id).filter(...).subquery()
    
    db.query(...).filter(A.id.in_(subquery))
    

    into:

    SELECT ...
    FROM a
    WHERE a.id IN (SELECT ...)
    

提交回复
热议问题