KZiovas
KZiovas

Reputation: 4719

How to update a specific value in all query results (rows) in SQLAlchemy?

I have an SQLAlchemy query and I wish to update a specific field in all the items returned from the query something like:

items = MyModel.query().filter(
        MyModel.status == 'OK',
    )
with db.managed_session() as s:
   items.update(status_date=datetime.today())

how can this be achieved?

Upvotes: 0

Views: 64

Answers (1)

Ektorr
Ektorr

Reputation: 309

I usually use:

session.query(MyModel).\
        filter(MyModel.status == 'OK').\
        update({'status_date': datetime.today()})

I think that in your example is missing the {} in the update.

Upvotes: 1

Related Questions