0

How do i use group_by in python peewee?

This is my table:

   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

If i write this in SQL it will be like:

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

This is what i have tried:

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

it gives me error when i run:

for q in query:
  print(q.order_id)

This Error shows up:

peewee.InternalError:current transaction is aborted, commands ignored until end of transaction block
Jin Tan
  • 518
  • 6
  • 19

1 Answers1

1

That error typically means that the query executed previously had an error, and now the transaction is in a bad state and needs to be rolled-back.

The documentation has examples of using group by: http://docs.peewee-orm.com/en/latest/peewee/querying.html#aggregating-records

coleifer
  • 24,887
  • 6
  • 60
  • 75