SqlAlchemy 1.4 and Sequences

384 views
Skip to first unread message

Francesca L

unread,
Aug 31, 2021, 3:29:41 AM8/31/21
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),
)
metadata.drop_all(bind=session.bind)
metadata.create_all(bind=session.bind)

class Item(object):
    pass

sqlalchemy.orm.mapper(Item, t_items)

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

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
    self._process_executemany_defaults()
  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()
session.add(item1)
session.flush()
item2 = Item()
session.add(item2)
session.flush()

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

Francesca Leon

Mike Bayer

unread,
Aug 31, 2021, 10:24:24 AM8/31/21
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

unread,
Aug 31, 2021, 10:31:15 AM8/31/21
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

unread,
Sep 1, 2021, 6:12:18 AM9/1/21
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
    self._flush(objects)
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\session.py", line 3438, in _flush
    transaction.rollback(_capture_exception=True)
  File "C:\Python36\lib\site-packages\sqlalchemy\util\langhelpers.py", line 72, in __exit__
    with_traceback=exc_tb,
  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
    flush_context.execute()
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 456, in execute
    rec.execute(self)
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 633, in execute
    uow,
  File "C:\Python36\lib\site-packages\sqlalchemy\orm\persistence.py", line 247, in save_obj
    insert,
  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
    cache_hit=cache_hit,
  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
    **kwargs
TypeError: execute_values() got an unexpected keyword argument 'fetch'

Mike Bayer

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