简体   繁体   English

为什么在使用pegreee ORM和posgresql的一个失败命令之后,为什么所有后续命令都出现peewee.InternalError?

[英]Why am I getting peewee.InternalError for all subsequent commands after one failed command, using peewee ORM with posgresql?

I have been working extensively with peewee and postgresql for months. 我已经与peewee和postgresql广泛合作了几个月。 Suddenly this started happening. 突然,这开始发生。 If I run any query command and get an error, then all subsequent commands start returning peewee.InternalError: current transaction is aborted, commands ignored until end of transaction block . 如果我运行任何查询命令并遇到错误,则所有后续命令都将开始返回peewee.InternalError: current transaction is aborted, commands ignored until end of transaction block

I thought this behavior started when I upgraded peewee from 3.5.2 to 3.7.2, but I have since downgraded and the behavior continues. 我以为将peewee从3.5.2升级到3.7.2时就开始出现这种行为,但是此后我将其降级了,并且这种行为仍在继续。 This has definitely not always happened. 这肯定并非总是发生。

In the simplest case, I have a database table with exactly one record. 在最简单的情况下,我有一个仅包含一条记录的数据库表。 I try to create a new record with the same id and I get an IntegrityError as expected. 我尝试创建具有相同ID的新记录,并且收到预期的IntegrityError If I then try to run any other query commands on that database, I get the InternalError as above. 如果然后尝试在该数据库上运行任何其他查询命令,则会收到如上所述的InternalError

This does not happen with an sqlite database. 使用sqlite数据库不会发生这种情况。

I have reinstalled peewee and psycopg2, to no avail. 我已经重新安装了peewee和psycopg2,但无济于事。

What am I missing? 我想念什么?

Try setting autorollback=True in Database class. 尝试在Database类中设置autorollback=True You can follow docs here 您可以在此处关注文档

Your problem is already known in this issue 您的问题在本期中已经知道

While it's fine to use autorollback, it's much better to explicitly manage your transactions so that where an integrity error might occur you are catching the error and explicitly rolling back. 尽管可以使用自动回滚,但显式管理事务要好得多,这样可以在发生完整性错误的地方捕获错误并显式回滚。 For instance, if you have a user signup page and there's a unique constraint on the username, you might wrap it in a try/except and rollback upon failure. 例如,如果您有一个用户注册页面,并且用户名存在唯一约束,则可以将其包装在try / except中,并在失败时回滚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM