DatabaseError: current transaction is aborted, com

2019-01-02 22:31发布

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.

16条回答
beautiful°
2楼-- · 2019-01-02 22:49

In Flask shell, all I needed to do was a session.rollback() to get past this.

查看更多
走好不送
3楼-- · 2019-01-02 22:52

just use rollback

Example code

try:
    cur.execute("CREATE TABLE IF NOT EXISTS test2 (id serial, qa text);")
except:
    cur.execute("rollback")
    cur.execute("CREATE TABLE IF NOT EXISTS test2 (id serial, qa text);")
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-02 22:52

you could disable transaction via "set_isolation_level(0)"

查看更多
劫难
5楼-- · 2019-01-02 22:55

In Flask you just need to write:

curs = conn.cursor()
curs.execute("ROLLBACK")
conn.commit()

P.S. Documentation goes here https://www.postgresql.org/docs/9.4/static/sql-rollback.html

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-02 22:58

To get rid of the error, roll back the last (erroneous) transaction after you've fixed your code:

from django.db import transaction
transaction.rollback()

You can use try-except to prevent the error from occurring:

from django.db import transaction, DatabaseError
try:
    a.save()
except DatabaseError:
    transaction.rollback()

Refer : Django documentation

查看更多
做自己的国王
7楼-- · 2019-01-02 22:58

If you get this while in interactive shell and need a quick fix, do this:

from django.db import connection
connection._rollback()

originally seen in this answer

查看更多
登录 后发表回答