【问题标题】:Peewee bulk_create return id'sPeewee bulk_create 返回 id
【发布时间】:2019-09-09 09:13:39
【问题描述】:

如果 peewee 中的记录来自 bulk_create,我需要返回 ids

我可以做这样的事情

from models import Table

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

我得到了一条新记录的 id

但如果我尝试

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

这里我得到一个错误:'t' is 'NoneType'

那么我如何从 peewee bulk_create 获取 id?

【问题讨论】:

    标签: python database orm peewee


    【解决方案1】:

    Peewee 确实返回ID列表如果您正在使用支持RETURNING子句的数据库。因此,如果您使用的是 Postgresql,那么 peewee 将返回 ID 列表。

    使用bulk_create() 的原因是因为它发出了一个高效的查询——一次插入许多行。 Sqlite 和 mysql 只提供“last insert id”。

    【讨论】:

      猜你喜欢
      • 2016-02-12
      • 2016-03-31
      • 1970-01-01
      • 2021-09-26
      • 2021-04-19
      • 2018-06-15
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      相关资源
      最近更新 更多