Consider this snippet. I am trying to get the user with the latest id like this:
JavaScript
x
2
1
User.query.get(func.max(User.id))
2
It throws an error saying Boolean value of this clause is not defined
How do I get the user with the largest id by using the model.query API?
Advertisement
Answer
I would solve it like:
JavaScript
1
2
1
last_user = User.query.order_by(User.id.desc()).first()
2
An alternative would be:
JavaScript
1
4
1
last_user = User.query.filter(
2
User.id == select(func.max(User.id)).scalar_subquery()
3
)
4