0

如何在 python peewee 中使用 group_by?

这是我的桌子:

   order_id    |              eatery_id
---------------+--------------------------------------
 1596949079845 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1596949235150 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1596949298783 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1596953145084 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1596953145084 | 184fa936-9835-449c-ac8c-fb72a789a96a
 1596953600173 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1596953600173 | 184fa936-9835-449c-ac8c-fb72a789a96a
 1596953600173 | 184fa936-9835-449c-ac8c-fb72a789a96a
 1597322217541 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1597322217541 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1597322217541 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1597322506231 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1597322506231 | 8e0c6adc-a65e-4a9b-95d2-82284299719d
 1597322506231 | 184fa936-9835-449c-ac8c-fb72a789a96a
 1597322506231 | 184fa936-9835-449c-ac8c-fb72a789a96a
 1596449173498 | 184fa936-9835-449c-ac8c-fb72a789a96a
 1596449778585 | 184fa936-9835-449c-ac8c-fb72a789a96a

如果我用 SQL 编写它,它会是这样的:

SELECT order_id
FROM table_name
WHERE eatery_id = '8e0c6adc-a65e-4a9b-95d2-82284299719d'
GROUP_BY order_id
 

这是我尝试过的:

query = Purchases.select().where(Purchases.eatery_id == '8e0c6adc-a65e-4a9b-95d2-82284299719d').group_by(Purchases.order_id)

当我运行时它给了我错误:

for q in query:
  print(q.order_id)

出现此错误:

peewee.InternalError:current transaction is aborted, commands ignored until end of transaction block
4

1 回答 1

1

该错误通常意味着之前执行的查询有错误,现在事务处于错误状态,需要回滚。

该文档有使用 group by 的示例:http: //docs.peewee-orm.com/en/latest/peewee/querying.html#aggregating-records

于 2020-08-14T00:56:57.830 回答