Hello everyone I write a bot in a telegram, get a list of chat users, then I need to write them to the database. I have three models of users, groups and user_groups. When recording, I get the following error:
"15.06.2024 01:08:36 - aiogram.event - [ERROR] - Cause exception while process update id=333211 by bot id=605730 MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)" Traceback (most recent call last): File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatcherdispatcher.py", line 309, in _process_update response = await self.feed_update(bot, update, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatcherdispatcher.py", line 158, in feed_update response = await self.update.wrap_outer_middleware( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatchermiddlewareserror.py", line 25, in __call__ return await handler(event, data) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatchermiddlewaresuser_context.py", line 49, in __call__ return await handler(event, data) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramfsmmiddleware.py", line 42, in __call__ return await handler(event, data) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatchereventtelegram.py", line 121, in trigger return await wrapped_inner(event, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "F:Python codedeveloptelegram_botappmiddlewaresmiddleware.py", line 20, in __call__ return await handler(event, data) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatchereventhandler.py", line 43, in call return await wrapped() ^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatcherdispatcher.py", line 276, in _listen_update return await self.propagate_event(update_type=update_type, event=event, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatcherrouter.py", line 142, in propagate_event return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatcherrouter.py", line 137, in _wrapped return await self._propagate_event( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatcherrouter.py", line 170, in _propagate_event response = await router.propagate_event(update_type=update_type, event=event, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatcherrouter.py", line 142, in propagate_event return await observer.wrap_outer_middleware(_wrapped, event=event, data=kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatcherrouter.py", line 137, in _wrapped return await self._propagate_event( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatcherrouter.py", line 162, in _propagate_event response = await observer.trigger(event, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatchereventtelegram.py", line 121, in trigger return await wrapped_inner(event, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagesaiogramdispatchereventhandler.py", line 43, in call return await wrapped() ^^^^^^^^^^^^^^^ File "F:Python codedeveloptelegram_botapphandlersbase_handlers.py", line 26, in start await crud_group.add_chat_members_to_db( File "F:Python codedeveloptelegram_botappcrudgroup.py", line 51, in add_chat_members_to_db UserGroupAssociation.group_id == group.group_id, ^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormattributes.py", line 566, in __get__ return self.impl.get(state, dict_) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormattributes.py", line 1086, in get value = self._fire_loader_callables(state, key, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormattributes.py", line 1116, in _fire_loader_callables return state._load_expired(state, passive) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormstate.py", line 796, in _load_expired self.manager.expired_attribute_loader(self, toload, passive) File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormloading.py", line 1653, in load_scalar_attributes result = load_on_ident( ^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormloading.py", line 509, in load_on_ident return load_on_pk_identity( ^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormloading.py", line 694, in load_on_pk_identity session.execute( File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormsession.py", line 2306, in execute return self._execute_internal( ^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormsession.py", line 2191, in _execute_internal result: Result[Any] = compile_state_cls.orm_execute_statement( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyormcontext.py", line 293, in orm_execute_statement result = conn.execute( ^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyenginebase.py", line 1421, in execute return meth( ^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemysqlelements.py", line 514, in _execute_on_connection return connection._execute_clauseelement( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyenginebase.py", line 1643, in _execute_clauseelement ret = self._execute_context( ^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyenginebase.py", line 1849, in _execute_context return self._exec_single_context( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyenginebase.py", line 1989, in _exec_single_context self._handle_dbapi_exception( File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyenginebase.py", line 2359, in _handle_dbapi_exception raise exc_info[1].with_traceback(exc_info[2]) File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyenginebase.py", line 1970, in _exec_single_context self.dialect.do_execute( File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyenginedefault.py", line 924, in do_execute cursor.execute(statement, parameters) File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemydialectssqliteaiosqlite.py", line 146, in execute self._adapt_connection._handle_exception(error) File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemydialectssqliteaiosqlite.py", line 298, in _handle_exception raise error File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemydialectssqliteaiosqlite.py", line 123, in execute _cursor = self.await_(self._connection.cursor()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:UsersAntonAppDataLocalProgramsPythonPython312Libsite-packagessqlalchemyutil_concurrency_py3k.py", line 118, in await_only raise exc.MissingGreenlet( sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)
from app.models.group import Group
from app.models.user_group_association import UserGroupAssociation
from sqlalchemy import select
from app.crud.base import CRUDBase
from app.crud.user_group_association import crud_user_group_association
from sqlalchemy.ext.asyncio import AsyncSession
from app.crud.user import crud_user
from aiogram import types
from app.models.user import User
class CRUDGroup(CRUDBase):
async def add_chat_members_to_db(
self, chat: types.Chat, user_data: User, session: AsyncSession
):
group = await session.execute(
select(self.model).where(self.model.group_id == chat.id)
)
group = group.scalars().first()
if not group:
group = await self.create(
{'group_id': chat.id, 'group_name': chat.title}, session
)
for user_info in user_data:
print(f'user_info >>>>>>>>>>>>>{user_info}')
user = await crud_user.get_user_id(user_info['user_id'], session)
if not user:
user = await crud_user.create(
{
'user_id': user_info['user_id'],
'user_name': user_info['user_name'],
'first_name': user_info['first_name'],
'last_name': user_info['last_name'],
},
session,
)
association = await session.execute(
select(UserGroupAssociation).where(
UserGroupAssociation.user_id == user.user_id,
UserGroupAssociation.group_id == group.group_id,
)
)
association = association.scalars().first()
if not association:
await crud_user_group_association.create(
{
'user_id': user.user_id,
'group_id': group.group_id,
'is_admin': user_info['is_admin_or_creator'],
},
session,
)
crud_group = CRUDGroup(Group)
I tried to add lazy="selectin"
in relationship
It didn’t help. What am I doing wrong?
Антон К is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.