我收到了很多错误的信息:
"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。
代码保持不变,只是不知道这些错误来自哪里。
当前回答
我也有这个错误,但它掩盖了另一个更相关的错误消息,代码试图在100个字符的列中存储125个字符的字符串:
DatabaseError: value too long for type character varying(100)
我必须调试代码才能显示上面的消息,否则就会显示
DatabaseError: current transaction is aborted
其他回答
我正在使用python包psycopg2,我在查询时得到了这个错误。 我一直只运行查询,然后运行execute函数,但是当我重新运行连接(如下所示)时,它解决了这个问题。所以重新运行什么是上面你的脚本即连接,因为有人说上面,我认为它失去了连接或不同步或其他。
connection = psycopg2.connect(user = "##",
password = "##",
host = "##",
port = "##",
database = "##")
cursor = connection.cursor()
在Flask中,你只需要写:
curs = conn.cursor()
curs.execute("ROLLBACK")
conn.commit()
附注:文档在这里https://www.postgresql.org/docs/9.4/static/sql-rollback.html
我也遇到了类似的问题。解决方案是迁移db (manage.py syncdb或manage.py schemmigration——auto <表名>如果您使用south)。
我认为在使用PostgreSQL时,priestc提到的模式更有可能是这个问题的常见原因。
然而,我觉得这个模式有一些有效的用途,我不认为这个问题应该成为总是避免它的理由。例如:
try:
profile = user.get_profile()
except ObjectDoesNotExist:
profile = make_default_profile_for_user(user)
do_something_with_profile(profile)
如果你觉得这种模式没问题,但又不想到处都是显式的事务处理代码,那么你可能会考虑开启自动提交模式(PostgreSQL 8.2+): https://docs.djangoproject.com/en/dev/ref/databases/#autocommit-mode
DATABASES['default'] = {
#.. you usual options...
'OPTIONS': {
'autocommit': True,
}
}
我不确定是否有重要的性能考虑因素(或任何其他类型)。
根据我的经验,这些错误是这样发生的:
try:
code_that_executes_bad_query()
# transaction on DB is now bad
except:
pass
# transaction on db is still bad
code_that_executes_working_query() # raises transaction error
第二个查询没有问题,但是由于捕获了真正的错误,第二个查询将引发(信息量少得多的)错误。
edit:这只发生在except子句捕捉到IntegrityError(或任何其他低级数据库异常)时,如果你捕捉到像DoesNotExist这样的错误,这个错误将不会出现,因为DoesNotExist不会破坏事务。
这里的教训是不要尝试/except/pass。