4

I'm using Peewee with Postgres database. I want to know how to update multiple records in a tabel at once?
We can perform this update in SQL using these commands, and I'm looking for a Peewee equivalent approach.

Community
  • 1
  • 1
Reyraa
  • 4,174
  • 2
  • 28
  • 54
  • 1
    Have you tried Model.update() method? – Gordian Dec 07 '16 at 22:26
  • Yes, but I'm wondering about performance. I've recently started learning and I red [here](http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.insert_many) that multiple times inserting a single item is wrong and I couldn't find a method similar to insert_many – Reyraa Dec 07 '16 at 22:30

2 Answers2

1

Yes, you can use the insert_many() function:

Insert multiple rows at once. The rows parameter must be an iterable that yields dictionaries. As with insert(), fields that are not specified in the dictionary will use their default value, if one exists.

Example:

usernames = ['charlie', 'huey', 'peewee', 'mickey']
row_dicts = ({'username': username} for username in usernames)

# Insert 4 new rows.
User.insert_many(row_dicts).execute()

More details at: http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.insert_many

mart1n
  • 5,969
  • 5
  • 46
  • 83
  • 8
    But this is inserting new items, I'd like to update existing records. – Reyraa Dec 08 '16 at 14:31
  • @alihaghighatkhah Then use a single `.update()` as Gordian mentioned. It runs as a single transaction, not several separate update operations. – mart1n Dec 08 '16 at 14:51
  • Thank you. yes eventually I changed my pipeline to use `update` method – Reyraa Dec 09 '16 at 16:53
-5

ORMs usually dose not support bulk update and you have to use custom SQL, you can see samples in this link (db.excute_sql)

Behrooz
  • 2,437
  • 3
  • 21
  • 31