SQLAlchemy and empty IN clause

前端 未结 5 573
爱一瞬间的悲伤
爱一瞬间的悲伤 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:32

    I'm using:

    if len(ids) > 0:
        db.query(...).where(A.id.in_(ids))
    else:
        db.query(...).where(False)
    

    I tried a .limit(0) instead of the .where(false) without success. There is some behind-the-scenes difference in the empty querysets that broke other stuff down the pipeline. This workaround, while could be faster, at least avoids your mentioned warning.

提交回复
热议问题