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

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

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

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


当前回答

您可以通过“set_isolation_level(0)”禁用事务

其他回答

你只需要跑

rollback;

在PostgreSQL中,就是这样!

当查询产生错误,而您试图在不先回滚事务的情况下运行另一个查询时,postgres就会这样做。(你可能会认为这是一个安全功能,防止你破坏你的数据。)

要解决这个问题,您需要找出在代码中执行错误查询的位置。在postgresql服务器中使用log_statement和log_min_error_statement选项可能会有所帮助。

我也遇到了同样的问题。我在这里遇到的问题是我的数据库没有正确地同步。简单的问题似乎总是引起最大的焦虑。

要同步你的django db,在你的app目录中,在终端中,输入:

$ python manage.py syncdb

编辑:注意,如果你正在使用django-south,运行'$ python manage.py migrate'命令也可以解决这个问题。

编码快乐!

您可以通过“set_isolation_level(0)”禁用事务

我遇到过这个问题,错误出现是因为错误事务没有正确结束,我在这里找到了事务控制命令的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