python - SQLAlchemy + postgres : (InternalError) current transaction is aborted, commands ignored until end of transaction block -
i attempting save parent/children set of records, , want wrap inserts in transaction. using sqlalchemy postgresql 8.4.
here snippet of code:
def insert_data(parent, child_rows): # start transaction conn = _get_connection() tran = conn.begin() try: sql = get_sql_from_parent(parent) res = conn.execute(sql) # <- code barfs @ line item = res.fetchone() if res else none parent_id = item['id'] if ((item) , ('id' in item)) else -1 if parent_id == -1: raise exception('parent not saved in database') # import children child in child_rows: child_sql = get_child_sql(parent_id, child) conn.execute(child_sql) tran.commit() except integrityerror: pass # rollback? except exception e: tran.rollback() print "exception in user code:" print '-'*60 traceback.print_exc(file=sys.stdout) print '-'*60 when invoke function, 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 aborted, commands ignored until end of transaction block ... does know why getting cryptic error - , how resolve it?
can activate log query on postgresql ? (min_duration set 0 in postgresql.conf restart).
then @ postgresql logs debug it.
Comments
Post a Comment