0

How to create similar operation in Peewee? This is SQLAlchemy code:

for record in records:
    session.delete(record)
session.commit()

This code delete all the records within one SQL delete statement.

I tried using following code. But it creates single delete statement for every record:

with db.atomic():
    for record in records:
        record.delete_instance()
Greex
  • 1
  • 3
  • Does this answer your question? [Deleting multiple records in a table using join in Peewee?](https://stackoverflow.com/questions/33290336/deleting-multiple-records-in-a-table-using-join-in-peewee) – Michael M. May 13 '23 at 15:46

1 Answers1

0

Yes, if you have a list of IDs, say:

list_of_ids = [r.id for r in records]
query = MyModel.delete().where(MyModel.id.in_(list_of_ids))
query.execute()

You can also, with some databases, use a subquery instead:

records = MyModel.select().where(...)
query = MyModel.delete().where(MyModel.id.in_(records))
query.execute()
coleifer
  • 24,887
  • 6
  • 60
  • 75