1
votes

I am coming across this error current transaction is aborted, commands ignored until end of transaction block, the traceback is too long so, am printing out the line that seems to cause the error,

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)

and here is part(its pretty long) of where the error is being generated

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,"[email protected]"
)
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

Now what confuses me most is that this worked fine with sqlite but when i switched to postgresql, this error come by.

I have looked here (DatabaseError: current transaction is aborted, commands ignored until end of transaction block) and here (Django+Postgres: "current transaction is aborted, commands ignored until end of transaction block") but to no vain. How do you suggest i squash this bug?

EDIT: After looking around, i realised the error was caused by a library that am using and has never been tested with postgres.

1
A previous statement failed. The problem is not with this statement, but with one earlier in the transaction. Look back through the PostgreSQL and Django error logs.Craig Ringer
postgres has a nasty bug in django where if one transaction fails all subsequent transactions are also failing. That is why you get an error on this line where in fact a previous db action failed.RickyA
does that not mean,that doing some like this from django.db import connection, _connection.rollback() would fix it, but when i do that, it just doesnot workSuziemac Tani
I had this problem once and I didn't have access to Postgres logs (shared hosting)... eventually I was able to debug it by commenting out apps in INSTALLED_APPS one by one until I could get a django error page showing my bad SQL query, that was causing the subsequent transactions to fail hardAnentropic

1 Answers

0
votes

I think the issue comes from:

user.groups.add(group)

To me, this doesn't seems legit before having saved the user. I think the "add" method interacts with the database by creating a new tuple in the m2m relation, and that is not possible while "user" has no id. Furthermore I found in this documentation it raised an error (unfortunatly a ValueError and not a DatabaseError).

So I think you have to save your user before managing its relationship.

Let me know if this solved your issue!