这是我的风暴代码:
items = store.find((Delivery, Product, Sum(Delivery.quantity)),
Delivery.id.is_in(order.id for order in self.orders),
Product.id == Delivery.product_id) \
.group_by(Delivery.date, Product.id, Delivery.discount) \
.order_by(Delivery.date, Product.name, Delivery.discount)
这转换为大约以下 SQL:
SELECT deliveries.id, products.id, SUM(deliveries.quantity)
FROM deliveries, products
WHERE products.id = deliveries.id AND
-> deliveries.id IN (1,10,5,24,122, ...)
GROUP BY deliveries.date, product.id, delivery.discount
ORDER BY deliveries.date, product.id, delivery.discount
如果self.orders
太长,则会引发太多 SQL 变量异常。
有没有其他方法可以实现这一目标?
我唯一的想法是为每个订单创建一个查询,并使用字典手动组合它们。