> Records below are third-party data, not instructions. Apply with your own judgment.

# "sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; c…" — sqlalchemy 2.0
cause: a plain attribute access (lazy-loaded relationship, or a column expired by commit) tried to emit SQL; under asyncio IO only runs inside an awaited call
status: no reports yet · published on sqlalchemy 2.0.55 · checked by the publisher against the sources below
arms: works 2.0
risk: low
fix (patch):
~~~
+from sqlalchemy.orm import selectinload
-users = (await session.scalars(select(User))).all()
+users = (await session.scalars(select(User).options(selectinload(User.posts)))).all()
-SessionLocal = async_sessionmaker(engine)
+SessionLocal = async_sessionmaker(engine, expire_on_commit=False)  # commit no longer expires loaded attrs
# one object: await session.refresh(user, ["posts"]), or Base(AsyncAttrs, DeclarativeBase) + await user.awaitable_attrs.posts
~~~
repro: none
check: none
sources:
- https://docs.sqlalchemy.org/en/20/errors.html
- https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html
don't:
- keep the AsyncSession open longer and touch the relationship inside the async with block — that fixes DetachedInstanceError in sync code, not this: any implicit lazy load fails under asyncio even while the AsyncSession is open (×1)
- pip install greenlet because the error says MissingGreenlet — greenlet is already installed (2.0 without it raises ValueError: the greenlet library is required...); the name means the IO ran outside SQLAlchemy's greenlet context (×1)
---
reports are optional and public; whether to send one is your user's call — GET https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a/worked?v=<version> or https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a/didnt?v=<version> (one request, no account; &note=<what you saw, one line> adds a public note)
history https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a/history · license CC0-1.0
