简体   繁体   English

SQLAlchemy + postgres:(InternalError)当前事务中止,命令被忽略,直到事务块结束

[英]SQLAlchemy + postgres : (InternalError) current transaction is aborted, commands ignored until end of transaction block

I am attempting to save a parent/children set of records, and I want to wrap the inserts in a transaction. 我正在尝试保存一组父/子记录,并且要将插入内容包装在事务中。 I am using SQLAlchemy with postgresql 8.4. 我在PostgreSQL 8.4中使用SQLAlchemy。

Here is a snippet of my code: 这是我的代码片段:

def insert_data(parent, child_rows):
    # Start a transaction
    conn = _get_connection()
    tran = conn.begin()

    try:
        sql = get_sql_from_parent(parent)
        res = conn.execute(sql)  # <- Code barfs at this line
        item = res.fetchone() if res else None
        parent_id = item['id'] if ((item) and ('id' in item)) else -1

        if parent_id == -1:
            raise Exception('Parent could not be saved in database')

        # Import children
        for child in child_rows:
            child_sql = get_child_sql(parent_id, child)                        
            conn.execute(child_sql)

        tran.commit()

    except IntegrityError:
        pass  # rollback?

    except Exception as e:
        tran.rollback()
        print "Exception in user code:"
        print '-'*60
        traceback.print_exc(file=sys.stdout)
        print '-'*60

When I invoke the function, I get the following stacktrace: 当我调用该函数时,得到以下堆栈跟踪:

Traceback (most recent call last):
  File "import_data.py", line 125, in <module>
    res = conn.execute(sql)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.7.4-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1405, in execute
    params)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.7.4-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1582, in _execute_text
    statement, parameters
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.7.4-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1646, in _execute_context
    context)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.7.4-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1639, in _execute_context
    context)
  File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.7.4-py2.6-linux-x86_64.egg/sqlalchemy/engine/default.py", line 330, in do_execute
    cursor.execute(statement, parameters)
InternalError: (InternalError) current transaction is aborted, commands ignored until end of transaction block
...

Does anyone know why I am getting this cryptic error - and how do I resolve it? 有谁知道为什么我会收到这个隐秘的错误-我该如何解决?

Can you activate the log query on postgresql ? 您可以在postgresql上激活日志查询吗? (min_duration set to 0 in postgresql.conf then restart). (在postgresql.conf中将min_duration设置为0,然后重新启动)。

Then look at your postgresql logs to debug it. 然后查看您的postgresql日志以对其进行调试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InternalError:当前事务中止,命令被忽略,直到事务块结束 - InternalError: current transaction is aborted, commands ignored until end of transaction block django-当前事务中止,命令被忽略,直到事务块结束 - django - current transaction is aborted, commands ignored until end of transaction block DatabaseError:当前事务被中止,在事务块结束之前忽略命令? - DatabaseError: current transaction is aborted, commands ignored until end of transaction block? 错误:当前事务中止,创建新记录时命令被忽略,直到事务块结束 - Error: current transaction is aborted, commands ignored until end of transaction block while creating a new record DatabaseError:当前事务中止,命令被忽略,直到事务块结束-在隐身模式下,但正常情况下没有错误 - DatabaseError: current transaction is aborted, commands ignored until end of transaction block - in incognite mode but no error in normal 如何调试:内部错误当前事务被中止,命令被忽略直到事务块结束 - How to debug: Internal Error current transaction is aborted, commands ignored until end of transaction block 使用 Python 连接到 Redshift 数据 - 错误:当前事务被中止,命令被忽略,直到事务块结束 - Connecting to Redshift Data Using Python - Error: current transaction is aborted, commands ignored until end of transaction block django reg extension-当前事务中止,命令被忽略,直到事务块结束 - django reg extend - current transaction is aborted, commands ignored until end of transaction block psycopg2.errors.InFailedSqlTransaction:当前事务被中止,命令被忽略直到事务块结束 - psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block Django:通过添加m2m导致“当前事务中止,命令被忽略,直到事务块结束” - Django: Added m2m via through causes 'current transaction is aborted, commands ignored until end of transaction block'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM