我收到了很多错误的信息:
"DatabaseError: current transaction is aborted, commands ignored until end of transaction block"
作为Django项目的数据库引擎,从python-psycopg改为python-psycopg2。
代码保持不变,只是不知道这些错误来自哪里。
我收到了很多错误的信息:
"DatabaseError: current transaction is aborted, commands ignored until end of transaction block"
作为Django项目的数据库引擎,从python-psycopg改为python-psycopg2。
代码保持不变,只是不知道这些错误来自哪里。
当前回答
在Flask中,你只需要写:
curs = conn.cursor()
curs.execute("ROLLBACK")
conn.commit()
附注:文档在这里https://www.postgresql.org/docs/9.4/static/sql-rollback.html
其他回答
我也有这个错误,但它掩盖了另一个更相关的错误消息,代码试图在100个字符的列中存储125个字符的字符串:
DatabaseError: value too long for type character varying(100)
我必须调试代码才能显示上面的消息,否则就会显示
DatabaseError: current transaction is aborted
这是一个糟糕的sql执行问题,它不允许其他查询执行,直到前一个查询被挂起/回滚。
在PgAdmin4-4.24中有一个回滚选项,可以尝试一下。
要摆脱错误,在你修复了你的代码后回滚最后一个(错误的)事务:
from django.db import transaction
transaction.rollback()
你可以使用try-except来防止错误的发生:
from django.db import transaction, DatabaseError
try:
a.save()
except DatabaseError:
transaction.rollback()
参考:Django文档
这里也有类似的错误。我在这个链接https://www.postgresqltutorial.com/postgresql-python/transaction/中找到了答案
client = PsqlConnection(config)
connection = client.connection
cursor = client.cursor
try:
for query in list_of_querys:
#query format => "INSERT INTO <database.table> VALUES (<values>)"
cursor.execute(query)
connection.commit()
except BaseException as e:
connection.rollback()
这样,你发送给postgresql的以下查询将不会返回错误。
我也遇到了类似的问题。解决方案是迁移db (manage.py syncdb或manage.py schemmigration——auto <表名>如果您使用south)。