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

时间:2013-04-19 09:11:26

标签: django django-models django-postgresql

我遇到此错误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"),但没有白费。  你怎么建议我压扁这个bug?

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

1 个答案:

答案 0 :(得分:0)

我认为问题来自:

user.groups.add(group)

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

所以我认为你必须在管理其关系之前保存用户。

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