首页 > 解决方案 > Peewee 主键未显示(失败行包含 null)

问题描述

我正在使用 peewee ORM 来管理一些 Postgres 数据库。我最近遇到了一个问题,即主键不会在应有的调用时save()或被execute()调用时自动添加。

这是被调用的代码: Macro.insert(name=name, display_text=text).on_conflict(conflict_target=(Macro.name,), preserve=(Macro.display_text,), update={Macro.name: name}).execute()

这是错误: Command raised an exception: IntegrityError: null value in column "id" violates non-null constraint; DETAIL: Failing row contains (null, nametexthere, displaytexthere)

宏类有一个id (AutoField [set to be primary key]), name (CharField), and display_text (CharField). 我已经尝试使用内置的 PrimaryKeyField 和一个 IntegerField 设置为主键以保持不变。

之前,我使用 Heroku 没有问题。从那以后,我将我的应用程序迁移到了我的 Raspberry Pi,这就是这个问题出现的时候。

这也不是我遇到此问题的唯一情况。我有另一个具有相同 AutoField 主键的数据库,从 Heroku 到 Pi 的转换似乎已经中断。那一个使用save()方法而不是insert()/execute(),但仍然出现失败的行错误。

还应该提到其他非插入查询工作正常。我仍然可以毫无问题地选择。

标签: pythonpostgresqlpeewee

解决方案


这个问题与 Peewee 没有任何关系,它与转储有关。Heroku 不会自动为您转储序列,因此我不得不手动重新添加它们。添加这些连接后,连接工作正常。


推荐阅读