I am unable to insert data using SQLAlchemy (v1.3.10) into a postgresql 11 table. I first noticed the error when running some old code that had been previously working with SQLAlchemy (v1.2.10) and posgresql 11. I updated both versions in my project some time ago, but hadn't tested the database part of my codebase until now (my mistake). The code base is relatively large, so unfortunately, I'm not exactly sure what else could have changed to cause this problem. I have tried downgrading sqlalchemy and psycopg2 to the previous versions, but that did not resolve the issue. Unfortunately, reverting back to postgresql 10 is more challenging so I would like to avoid that if possible.
The error first occurred when trying to insert a ORM model object, but I get the same error when trying to insert using the expression API and even with raw sql statements. I set up a very simple table:
create table test_table(
id integer not null primary key;
);
and created a session using the Getting a Session recipe with a context manager (lower on the page). When executing this code:
stmt = text("insert into nba.test_table(id) values (:id)")
values = {'id': 3}
session.execute(stmt, values)
I get the following stack trace:
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)
The stack trace is nearly identical whether I use a raw sql query, a query built using the sqlalchemy expression api, or using a model class mapped with the ORM api.
I can query tables just fine and if I run an equivalent query using the psycopg2 module directly, I have no problems inserting the data.