gpt4 book ai didi

python - Peewee bulk_create 返回 ID

转载 作者:搜寻专家 更新时间:2023-10-30 23:25:58 25 4
gpt4 key购买 nike

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

我可以做这样的事

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?

最佳答案

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

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

关于python - Peewee bulk_create 返回 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57851013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com