我无法使用SQLAlchemy(v1.3.10)将数据插入到postgresql 11表中。我在运行一些以前使用过SQLAlchemy(v1.2.10)和posgresql 11的旧代码时首先注意到了该错误。我前一段时间在项目中更新了这两个版本,但直到现在还没有测试我的代码库的数据库部分(我的错)。代码库相对较大,因此,不幸的是,我不确定是否还有其他更改会导致此问题。我曾尝试将sqlalchemy和psycopg2降级到以前的版本,但这不能解决问题。不幸的是,恢复到postgresql 10更具挑战性,因此,我尽可能避免这种情况。
尝试插入ORM模型对象时首先发生该错误,但是当尝试使用表达式API甚至使用原始sql语句进行插入时,也会遇到相同的错误。我建立了一个非常简单的表:
create table test_table(
id integer not null primary key;
);
,并使用Getting a Session配方和上下文管理器(在页面下方)创建了会话。执行此代码时:
stmt = text("insert into nba.test_table(id) values (:id)")
values = {'id': 3}
session.execute(stmt, values)
我得到以下堆栈跟踪:
Traceback (most recent call last):
File "/usr/lib64/python3.6/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/lib64/python3.6/unittest/case.py", line 605, in run
testMethod()
File "/home/reid/git/worthy/test/test_db.py", line 32, in test_insert
session.execute(stmt, values)
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/orm/session.py", line 1269, in execute
clause, params or {}
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 988, in execute
return meth(self, multiparams, params)
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
distilled_params,
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 1253, in _execute_context
e, statement, parameters, cursor, context
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 1473, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/engine/base.py", line 1249, in _execute_context
cursor, statement, parameters, context
File "/home/reid/git/worthy/.venv/lib64/python3.6/site-packages/sqlalchemy/engine/default.py", line 580, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "insert"
LINE 1: ...ECLARE "c_7fbd3ae7c8d0_1" CURSOR WITHOUT HOLD FOR insert int...
^
[SQL: insert into nba.test_table(id) values (%(id)s)]
[parameters: {'id': 3}]
(Background on this error at: http://sqlalche.me/e/f405)
无论我使用原始sql查询,使用sqlalchemy表达式api构建的查询,还是使用通过ORM api映射的模型类,堆栈跟踪都几乎相同。
我可以很好地查询表,如果直接使用psycopg2模块运行等效查询,则插入数据没有问题。
答案 0 :(得分:0)
我创建引擎的方式与上面链接中提供的配方略有不同。
在链接中是:
some_engine = create_engine('postgresql://scott:tiger@localhost/')
我在其中加上了一个额外的论点
some_engine = create_engine('postgresql://scott:tiger@localhost/', execution_options={'stream_results': True})
删除execution_options
参数时,一切都会恢复。