Using Flask-SQLAlchemy, I have two tables - basket and product.
**basket**
id
basket_type
total_amount
created
**product**
id
product_type
price
basket_id
created
I am trying to query the basket with more than "some_number" products. How to do it in one database query?
I tried to do it just like that:
results = db.session.query(Basket, func.count(Product.basket_id)).\
join(Product, Basket.id == Product.basket_id).group_by(Product.basket_id).all()
I'm getting a list of tuples like:
[(<basket 1>, 25), (<basket 2>, 10)...]
then:
for res in results:
if res[1] > 'some_number':
print(res[0].id, res[0].basket_type) # actually, the basket append in list for next using in flask-app
How to do it in one database query?? I want the check to be performed in the request immediately.