1
votes

I happen to run into a problem which I've never seen before using sqlalchemy based on the same ORM schema. Here's a snippet of code.

@contextmanager
def session_scope():
    """ provide a transaction scope around a series of operations """
    session = db_session()
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()

with session_scope() as session:
    query = (session
             .query(*orm_lst)
             .filter(and_(*filter_lst)).group_by(*group_by_lst)
             .order_by(*order_by_lst))

When I look into the query variable, there's a statement variable saying AttributeError: 'int' object has no attribute '_set_parent_with_dispatch'. And here's part of traceback.

Traceback (most recent call last): ...
File "D:\software\conda\lib\site-packages\sqlalchemy\orm\query.py", line 474, in statement return stmt._annotate({'no_replacement_traverse': True})
File "D:\software\conda\lib\site-packages\sqlalchemy\sql\elements.py", line 235, in _annotate return Annotated(self, values)
File "D:\software\conda\lib\site-packages\sqlalchemy\sql\selectable.py", line 3712, in init element.c
File "D:\software\conda\lib\site-packages\sqlalchemy\util\langhelpers.py", line 764, in get obj.dict[self.name] = result = self.fget(obj)
File "D:\software\conda\lib\site-packages\sqlalchemy\sql\selectable.py", line 686, in columns self._populate_column_collection()
File "D:\software\conda\lib\site-packages\sqlalchemy\sql\selectable.py", line 3462, in _populate_column_collection name_is_truncatable=True)
File "D:\software\conda\lib\site-packages\sqlalchemy\sql\elements.py", line 3590, in _make_proxy name=name if name else self.name)
File "D:\software\conda\lib\site-packages\sqlalchemy\sql\schema.py", line 1445, in _make_proxy _proxies=[self], *fk)
File "D:\software\conda\lib\site-packages\sqlalchemy\sql\schema.py", line 1248, in init self._init_items(*args)
File "D:\software\conda\lib\site-packages\sqlalchemy\sql\schema.py", line 79, in _init_items item._set_parent_with_dispatch(self)
AttributeError: 'int' object has no attribute '_set_parent_with_dispatch'

Please, anyone can help?

1
What is in orm_lst?snakecharmerb
there're might be various columns to be in select from. I refer variable of list type as _lst.TAOGS
Similar stackoverflow.com/q/31443864/5320906; the problem was a name collision. It would be helpful if you could create an minimal reproducible example; the sample code is too generic.snakecharmerb

1 Answers

0
votes

This blur error turns out to be that select XXX as xxx from ... has a int-type variable treated as alias of the column name when using ORM. I hope it helps since AttributeError: 'int' object has no attribute '_set_parent_with_dispatch' could occur under various circumstances.