SqlAlchemy 1.4 and Sequences

Skip to first unread message

Francesca L

Aug 31, 2021, 3:29:41 AMAug 31
to sqlalchemy
Hi group,

I am tring to migrate from version 1.3.24 to 1.4.23 of SqlAlchemy, using PostgreSQL 10.

I found that the following code example works with 1.3, but triggers a traceback with 1.4.

import sqlalchemy

session = ...
metadata = sqlalchemy.MetaData()
s_items = sqlalchemy.Sequence('s_items', start=1, increment=1, metadata=metadata)
t_items = sqlalchemy.Table('t_items', metadata,
    sqlalchemy.Column('id', sqlalchemy.Integer, s_items, primary_key = True),

class Item(object):

sqlalchemy.orm.mapper(Item, t_items)

item1 = Item()
item2 = Item()
session.add_all([item1, item2])

This code, with 1.4 only, triggers the following traceback:

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\sqlalchemy\engine\base.py", line 1672, in _execute_context
    dialect, self, conn, execution_options, *args, **kw
  File "C:\Python36\lib\site-packages\sqlalchemy\engine\default.py", line 999, in _init_compiled
  File "C:\Python36\lib\site-packages\sqlalchemy\engine\default.py", line 1838, in _process_executemany_defaults
    if c.default and c.default.is_scalar:
AttributeError: 'Sequence' object has no attribute 'is_scalar'

The only way I found to work around this with version 1.4, is to call flush after each single add:

item1 = Item()
item2 = Item()

But this seems weird, to me.
Am I doing anything wrong?
Thank you very much for any suggestion.

Francesca Leon

Mike Bayer

Aug 31, 2021, 10:24:24 AMAug 31
to noreply-spamdigest via sqlalchemy
Hi -

Your test case is omitting a critical detail which we would assume is you are using the "implicit_returning=False" flag on your create_engine().    dont use this flag as it serves no useful purpose and that will fix your issue here, we will be deprecating and removing this flag.

will try to fix the immediate issue if possible.
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Mike Bayer

Aug 31, 2021, 10:31:15 AMAug 31
to noreply-spamdigest via sqlalchemy
issue https://github.com/sqlalchemy/sqlalchemy/issues/6963 is added to deal with possibly multiple failure modes in this case.

Francesca L

Sep 1, 2021, 6:12:18 AMSep 1
to sqlalchemy
Thank you very much for your quick help.

Yes, you are right, I was using the: "implicit_returning=False" flag on my "create_engine()".

I add the following, in case it can be useful to you:
I removed the flag as suggested, and that's what I get now for the same code example (with sqlalchemy 1.4):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\session.py", line 3298, in flush
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\session.py", line 3438, in _flush
  File "C:\Python36\lib\site-packages\sqlalchemy\util\langhelpers.py", line 72, in __exit__
  File "C:\Python36\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\session.py", line 3398, in _flush
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 456, in execute
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 633, in execute
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\persistence.py", line 247, in save_obj
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\persistence.py", line 1154, in _emit_insert_statements
    statement, multiparams, execution_options=execution_options
  File "C:\Python36\lib\site-packages\sqlalchemy\engine\base.py", line 1583, in _execute_20
    return meth(self, args_10style, kwargs_10style, execution_options)
  File "C:\Python36\lib\site-packages\sqlalchemy\sql\elements.py", line 324, in _execute_on_connection
    self, multiparams, params, execution_options
  File "C:\Python36\lib\site-packages\sqlalchemy\engine\base.py", line 1462, in _execute_clauseelement
  File "C:\Python36\lib\site-packages\sqlalchemy\engine\base.py", line 1815, in _execute_context
    e, statement, parameters, cursor, context
  File "C:\Python36\lib\site-packages\sqlalchemy\engine\base.py", line 1999, in _handle_dbapi_exception
    util.raise_(exc_info[1], with_traceback=exc_info[2])
  File "C:\Python36\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
    raise exception
  File "C:\Python36\lib\site-packages\sqlalchemy\engine\base.py", line 1752, in _execute_context
    cursor, statement, parameters, context
  File "C:\Python36\lib\site-packages\sqlalchemy\dialects\postgresql\psycopg2.py", line 968, in do_executemany
TypeError: execute_values() got an unexpected keyword argument 'fetch'

Mike Bayer

Sep 1, 2021, 9:42:44 AMSep 1
to noreply-spamdigest via sqlalchemy
please upgrade to the most recent psycopg2
Reply all
Reply to author
0 new messages