sqlalchemy_aio icon indicating copy to clipboard operation
sqlalchemy_aio copied to clipboard

AttributeError: 'AsyncioEngine' object has no attribute '_contextual_connect'.

Open vhermecz opened this issue 5 years ago • 0 comments

Trying to run an import process via sqlalchemy_aio and python await/async. When running self.session.query(City).filter_by(name='x').first(), the code fails with the AttributeError exception.

It might be as simple as this part of SQLA is not currently supported by sqlalchemy_aio. If so, it would be nice to have supported/unsupported parts of SQLA documented under the Limitations page. Thanks!

Stacktrace, Model definition and SQLA setup is included below:

  File "/app/importer/datacontext.py", line 52, in lookup
    city = self.session.query(City).filter_by(name=value.lower(), country_code=value_context.alpha_3.lower()).first()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3402, in first
    ret = list(self[0:1])
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3176, in __getitem__
    return list(res)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3508, in __iter__
    return self._execute_and_instances(context)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3530, in _execute_and_instances
    querycontext, self._connection_from_session, close_with_result=True
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3545, in _get_bind_args
    mapper=self._bind_mapper(), clause=querycontext.statement, **kw
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3523, in _connection_from_session
    conn = self.session.connection(**kw)
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1141, in connection
    execution_options=execution_options,
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1147, in _connection_for_bind
    engine, execution_options
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 433, in _connection_for_bind
    conn = bind._contextual_connect()
  File "/usr/local/lib/python3.6/site-packages/sqlalchemy_aio/base.py", line 217, in __getattr__
    raise AttributeError(msg)
AttributeError: 'AsyncioEngine' object has no attribute '_contextual_connect'.
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Numeric, ForeignKey
Base = declarative_base()

class City(Base):
    __tablename__ = 'cities'
    city_id = Column(Integer, primary_key=True)
    name = Column(String)
    region_code = Column(String)
    country_code = Column(String)
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_aio import ASYNCIO_STRATEGY

class DataContext(TransformationDataContext):
    def __init__(self, dburl):
        engine = create_engine(dburl, echo=True, strategy=ASYNCIO_STRATEGY)
        Session = sessionmaker(bind=engine)
        self.session = Session()

vhermecz avatar Aug 19 '20 12:08 vhermecz