I got a lot of errors with the message :
"DatabaseError: current transaction is aborted, commands ignored until end of transaction block"
after changed from python-psycopg to python-psycopg2 as Django project's database engine.
The code remains the same, just dont know where those errors are from.
I believe @AnujGupta's answer is correct. However the rollback can itself raise an exception which you should catch and handle:
If you find you're rewriting this code in various
save()
locations, you can extract-method:Finally, you can prettify it using a decorator that protects methods which use
save()
:Even if you implement the decorator above, it's still convenient to keep
try_rolling_back()
as an extracted method in case you need to use it manually for cases where specific handling is required, and the generic decorator handling isn't enough.I think the pattern priestc mentions is more likely to be the usual cause of this issue when using PostgreSQL.
However I feel there are valid uses for the pattern and I don't think this issue should be a reason to always avoid it. For example:
If you do feel OK with this pattern, but want to avoid explicit transaction handling code all over the place then you might want to look into turning on autocommit mode (PostgreSQL 8.2+): https://docs.djangoproject.com/en/dev/ref/databases/#autocommit-mode
I am unsure if there are important performance considerations (or of any other type).
I encountered a similar behavior while running a malfunctioned transaction on the
postgres
terminal. Nothing went through after this, as thedatabase
is in a state oferror
. However, just as a quick fix, if you can afford to avoidrollback transaction
. Following did the trick for me:COMMIT;
This is what postgres does when a query produces an error and you try to run another query without first rolling back the transaction. (You might think of it as a safety feature, to keep you from corrupting your data.)
To fix this, you'll want to figure out where in the code that bad query is being executed. It might be helpful to use the log_statement and log_min_error_statement options in your postgresql server.
So, I ran into this same issue. The problem I was having here was that my database wasn't properly synced. Simple problems always seem to cause the most angst...
To sync your django db, from within your app directory, within terminal, type:
Edit: Note that if you are using django-south, running the '$ python manage.py migrate' command may also resolve this issue.
Happy coding!
I've got the silimar problem. The solution was to migrate db (
manage.py syncdb
ormanage.py schemamigration --auto <table name>
if you use south).