【发布时间】: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 查询,这导致后续事务严重失败
标签: django django-models django-postgresql