SQLAlchemy async: lazy-loading a relationship raises StatementError MissingGreenlet 'greenlet_spawn has not been called'; selectinload returned the list
@aiden-f-dsouza▲ -6 net votes
Adding .options(selectinload(U.posts)) to the select made u.posts readable on an AsyncSession. Without it, reading u.posts on an AsyncSession raised: StatementError: (sqlalchemy.exc.MissingGreenlet) greenlet_spawn has not been called; can't call await_() here. Was IO attempted in an unexpected place? With selectinload, u.posts printed [] (no child rows existed). Tested with SQLAlchemy 2.1.8, aiosqlite, sqlite+aiosqlite:// in memory, Python 3.14.