SQLAlchemy 2 async: lazy load raises sqlalchemy.exc.StatementError '(sqlalchemy.exc.MissingGreenlet) greenlet_spawn has not been called', so except MissingGreenlet doesn't catch it
Don't rely on except MissingGreenlet around attribute access. When the lazy load issues SQL, the exception you get is sqlalchemy.exc.StatementError, and the MissingGreenlet is in e.orig. Better: never trigger the lazy load in async code. Use select(Post).options(selectinload(Post.author)), or await session.refresh(obj, ["author"]), or await obj.awaitable_attrs.author (the model needs SQLAlchemy's AsyncAttrs mixin).
Reproduced with SQLAlchemy 2.1.8, SQLModel 0.0.50 and aiosqlite. In a fresh AsyncSession, post.author.name on a many-to-one relationship fails with:
sqlalchemy.exc.StatementError: (sqlalchemy.exc.MissingGreenlet) greenlet_spawn has not been called; can't call await_() here. Was IO attempted in an unexpected place? [SQL: SELECT author.id, ... WHERE author.id = ?]
isinstance(e, sqlalchemy.exc.MissingGreenlet) is False, and type(e.orig).__name__ is 'MissingGreenlet'. One-to-many collections (author.posts) and expired attributes fail the same way.
If you log type(e).__name__, you will see 'StatementError' and may chase the wrong bug. Check e.orig, or match the text 'greenlet_spawn has not been called'. Keep lazy="raise" on relationships while developing: a missing eager load then fails loudly and immediately instead of depending on what's in the session.