如何调试内部错误当前事务被中止,命令被忽略直到事务块结束?

Posted

技术标签:

【中文标题】如何调试内部错误当前事务被中止,命令被忽略直到事务块结束?【英文标题】:How to debug a Internal Error current transaction is aborted, commands ignored until end of transaction block? 【发布时间】:2013-04-12 15:22:25 【问题描述】:

我遇到了这个错误current transaction is aborted, commands ignored until end of transaction block, 回溯太长,所以打印出似乎导致错误的行,

File "/home/matsinvasion/food4less/restaurant_detail/views.py" in pre_save
  176.          user=User.objects.create(username=obj.restaurant_detail.email,email=obj.restaurant_detail.email)

这是生成错误的部分(很长)

user=User.objects.create(username=obj.restaurant_detail.email,email=obj.restaurant_detail.email)

user.email_user(
    "F4L account activation",
    "Your membership to F4L has been approved go to the following link to activate your account \n \n http://f4l.herokuapp.com/restaurant_detail/restaurant/activate/%s/ \n \n After you have activated your account, login using your full email address as username and your password \n \n And customize what will be published on your F4L website profile by click profile link on the left of members \n \n This email is generated by the F4L website do not reply to it. " % obj.token,"website@f4l.herokuapp.com"
)
group=Group.objects.get(name='Restaurants')
user.groups.add(group)
user.first_name=userapp.first_name
user.last_name=userapp.last_name
user.email=userapp.email
user.set_unusable_password()
user.save()

obj.user=user
obj.is_active=False
obj.save()
return obj

现在最让我困惑的是,这在 sqlite 上运行良好,但是当我切换到 postgresql 时,出现了这个错误。

我看过这里 (DatabaseError: current transaction is aborted, commands ignored until end of transaction block) 和这里 (Django+Postgres: "current transaction is aborted, commands ignored until end of transaction block") 但没有白费。 你建议我如何解决这个错误?

编辑:环顾四周后,我意识到错误是由正在使用且从未使用 postgres 测试过的库引起的。

【问题讨论】:

上一条语句失败。问题不在于此声明,而在于事务中较早的声明。回顾 PostgreSQL 和 Django 错误日志。 postgres 在 django 中有一个讨厌的错误,如果一个事务失败,所有后续事务也会失败。这就是为什么您会在这一行收到错误,实际上之前的 db 操作失败了。 这并不意味着,像这样from django.db import connection, _connection.rollback() 会修复它,但是当我这样做时,它就不起作用了 我曾经遇到过这个问题,我无法访问 Postgres 日志(共享主机)...最终我能够通过在 INSTALLED_APPS 中逐一注释掉应用程序来调试它,直到我可能会出现一个 django 错误页面,显示我的错误 SQL 查询,这导致后续事务严重失败 【参考方案1】:

我认为问题出在:

user.groups.add(group)

对我来说,在拯救用户之前,这似乎并不合法。我认为“add”方法通过在 m2m 关系中创建一个新元组来与数据库交互,而当“user”没有 id 时这是不可能的。此外,我在this documentation 中发现它引发了一个错误(不幸的是 ValueError 而不是 DatabaseError)。

所以我认为你必须先保存你的用户,然后再管理它的关系。

如果这解决了您的问题,请告诉我!

【讨论】:

以上是关于如何调试内部错误当前事务被中止,命令被忽略直到事务块结束?的主要内容,如果未能解决你的问题,请参考以下文章

错误:当前事务被中止,在创建新记录时忽略命令直到事务块结束

DatabaseError:当前事务被中止,命令被忽略直到事务块结束 - 在隐身模式但正常没有错误

InternalError:当前事务被中止,命令被忽略直到事务块结束

使用 Python 连接到 Redshift 数据 - 错误:当前事务被中止,命令被忽略,直到事务块结束

Databene Benerator:org.postgresql.util.PSQLException:错误:当前事务被中止,命令被忽略,直到事务块结束

Django:通过添加 m2m 导致“当前事务被中止,命令被忽略,直到事务块结束”