Python supports chained comparisons: 1 < 2 < 3
translates to (1 < 2) and (2 < 3)
.
I am trying to make an SQL query using SQLAlchemy which looks like this:
results = session.query(Couple).filter(10 < Couple.NumOfResults < 20).all()
The results I got were not as expected. I've turned the engine's echo=True
keyword, and indeed - the generated SQL query only included one of the two comparisons.
I can't find any documentation that explicitly says this is forbidden. I assumed that if this type of expression is supported in Python, it should be supported in SQLAlchemy as well.
Why doesn't this work? I have one possible solution in mind (shared in answers), but will be glad to hear other opinions.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…