我收到了很多错误的信息:

"DatabaseError: current transaction is aborted, commands ignored until end of transaction block"

作为Django项目的数据库引擎,从python-psycopg改为python-psycopg2。

代码保持不变,只是不知道这些错误来自哪里。


当前回答

我也遇到了类似的问题。解决方案是迁移db (manage.py syncdb或manage.py schemmigration——auto <表名>如果您使用south)。

其他回答

作为对@priestc和@Sebastian的回应,如果你也这样做呢?

try:
    conn.commit()
except:
    pass

cursor.execute( sql )
try: 
    return cursor.fetchall()
except: 
    conn.commit()
    return None

我刚刚尝试了这段代码,它似乎工作,失败无声,而不必关心任何可能的错误,并在查询良好时工作。

如果你在交互式shell中得到这个,需要快速修复,请这样做:

from django.db import connection
connection._rollback()

最初见于这个答案

我在postgres终端上运行故障事务时遇到了类似的行为。在此之后什么都没有通过,因为数据库处于错误状态。但是,作为一个快速解决方案,如果可以避免回滚事务。以下是我的诀窍:

提交;

在Flask中,你只需要写:

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

附注:文档在这里https://www.postgresql.org/docs/9.4/static/sql-rollback.html

我遇到过这个问题,错误出现是因为错误事务没有正确结束,我在这里找到了事务控制命令的postgresql_transactions

事务控制

下面的命令用于控制事务

BEGIN TRANSACTION − To start a transaction.

COMMIT − To save the changes, alternatively you can use END TRANSACTION command.

ROLLBACK − To rollback the changes.

所以我使用END TRANSACTION来结束错误TRANSACTION,代码如下:

    for key_of_attribute, command in sql_command.items():
        cursor = connection.cursor()
        g_logger.info("execute command :%s" % (command))
        try:
            cursor.execute(command)
            rows = cursor.fetchall()
            g_logger.info("the command:%s result is :%s" % (command, rows))
            result_list[key_of_attribute] = rows
            g_logger.info("result_list is :%s" % (result_list))
        except Exception as e:
            cursor.execute('END TRANSACTION;')
            g_logger.info("error command :%s and error is :%s" % (command, e))
    return result_list