pythondatabaseormpeewee

Peewee bulk_create return id's


I need to return ids if the record in peewee from bulk_create

I can do something like that

from models import Table

t = Table.create(**data)
print(i.id)

and i get the id of a new record

But if i try

t = Table.bulk_create(list[**data])
for i in t:
    print(i.id)

here i get an error: 't' is 'NoneType'

So how can i get ids form peewee bulk_create?


Solution

  • Peewee does return the list of IDs if you are using a database that supports the RETURNING clause. So, if you're using Postgresql, then peewee will return the list of IDs.

    The reason to use bulk_create() is because it issues an efficient query -- inserting many rows at once. Sqlite and mysql only offer "last insert id".