Search code examples
peewee

Bulk updating records with peewee FlaskDb


I'm wanting to update hundreds (or even thousands) of records at a time with Peewee FlaskDb (which is in an App Factory).

Referencing the Peewee documentation, I have bulk_update working well (and it is very fast compared to other methods), but it fails when using batches.

For example, Ticket.bulk_update(selected_tickets, fields=[Ticket.customer]) works great, but when I use the following code to update in batches I receive the following error.

code

    with db.atomic():
        Ticket.bulk_update(selected_tickets, fields=[Ticket.customer], batch=50)

error

    AttributeError: 'FlaskDB' object has no attribute 'atomic'

What is the recommended way of updating records in bulk with FlaskDB? Does FlaskDB support atomic?


Solution

  • You are trying to access peewee.Database methods on the FlaskDB wrapper class. Those methods do not exist, you need to refer to the underlying Peewee database:

    # Here we assume db is a FlaskDB() instance:
    peewee_db = db.database
    
    with peewee_db.atomic():
        ...