3

During the execution of this function, I sometimes get an error: 'NoneType' object has no attribute ' twophase' in sqlalchemy.

    async def update_category_in_db(self, id_okolo: str, body: dict):
        await async_db_session.init()
        try:
            query = select(Category).where(Category.id_ras == body['categories'[0]['article']).join(Partner).where(
                Partner.code_okolo == body['partner'])
            result = await async_db_session.execute(query)
            product = result.scalars().first()
            product.id_okolo = id_okolo
            await async_db_session.commit()
            return True
        except Exception as e:
            logger.error(
                f'ERROR {e} in file: {os.path.abspath(__file__)} method: update_category_in_db')
        finally:
            await async_db_session.conn_close()

I will be grateful for any ideas or hints.

DB-PostgreSQL 13.3 Driver-asyncpg

This is my session object:

class AsyncDatabaseSession(object):
    def __init__(self):
        self._session = None
        self._engine = None

    def __getattr__(self, name):
        return getattr(self._session, name)

    async def init(self):
        self._engine = create_async_engine(
            'postgresql+asyncpg://{user}:{password}@{host}:{port}/{db}'.format(
                user=os.getenv('POSTGRES_USER'),
                password=os.getenv('POSTGRES_PASSWORD'),
                host=os.getenv('POSTGRES_HOST'),
                port=os.getenv('POSTGRES_PORT'),
                db=os.getenv('POSTGRES_DB')
            )
        )
        self._session = sessionmaker(
            self._engine, expire_on_commit=False, class_=AsyncSession,
        )()

async_db_session = AsyncDatabaseSession()

I also have a traceback from which little is clear to me..where could the session have gone?

Traceback (most recent call last):
  File "/src/okolo_server/nomenclature.py", line 81, in update_category_in_db
    await Product.update(product.id, id_okolo=id_okolo)
  File "/src/models/mixin.py", line 21, in update
    await async_db_session.execute(query)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/ext/asyncio/session.py", line 145, in execute
    **kw
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 122, in greenlet_spawn
    result = context.switch(value)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1676, in execute
    conn = self._connection_for_bind(bind, close_with_result=True)
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 1527, in _connection_for_bind
    engine, execution_options
  File "/usr/local/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 754, in _connection_for_bind
    if self.session.twophase and self._parent is None:
AttributeError: 'NoneType' object has no attribute 'twophase'

I also have a traceback from which little is clear to me..where could the session have gone?

Igor
  • 31
  • 3

0 Answers0