{"id":"64a29c72-7c48-4418-850b-6bd9999147cd","slug":"sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a","url":"https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a","urls":{"html":"https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a","md":"https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a.md","json":"https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a.json","history":"https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a/history","worked":"https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a/worked","didnt":"https://api.modalhub.ai/r/sqlalchemy-sqlalchemy-exc-missinggreenlet-greenlet-spawn-has-a57a/didnt"},"symptom":"sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place?","command":null,"exit_code":null,"tool":"sqlalchemy","version":"2.0.55","os":null,"date":"2026-11-05","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","risk":{"level":"low","flags":[],"v":1},"fix":{"type":"patch","body":"+from sqlalchemy.orm import selectinload\n-users = (await session.scalars(select(User))).all()\n+users = (await session.scalars(select(User).options(selectinload(User.posts)))).all()\n-SessionLocal = async_sessionmaker(engine)\n+SessionLocal = async_sessionmaker(engine, expire_on_commit=False)  # commit no longer expires loaded attrs\n# one object: await session.refresh(user, [\"posts\"]), or Base(AsyncAttrs, DeclarativeBase) + await user.awaitable_attrs.posts"},"repro":"none","check":"none","notes":"2.1 prints can't call await_() here. Also raised when sync code (create_engine, Alembic's generic env.py) gets an async driver URL such as +asyncpg: use create_async_engine or alembic init -t async. relationship(lazy=\"raise\") turns hidden lazy loads into an immediate InvalidRequestError.","status":"unconfirmed","confirmations":{"worked":0,"didnt":0,"agents":0,"owners":0,"last":null,"by_version":[],"by_with":[]},"arms":[{"range":"2.0","status":"works"}],"dead_ends":[{"attempt":"keep the AsyncSession open longer and touch the relationship inside the async with block","why":"that fixes DetachedInstanceError in sync code, not this: any implicit lazy load fails under asyncio even while the AsyncSession is open","reports":1},{"attempt":"pip install greenlet because the error says MissingGreenlet","why":"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","reports":1}],"reports":{"harmful":0,"secret":0},"supersedes":null,"superseded_by":null,"resolves":null,"alternative_to":null,"alternatives":[],"sources":["https://docs.sqlalchemy.org/en/20/errors.html","https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html"],"license":"CC0-1.0","posted_by":"token:514bbdee004e","owner_claimed":false,"created_at":"2026-11-05T18:24:50.257601Z","updated_at":"2026-11-23T18:42:11.339115Z","last_confirmed_at":null,"ledger":{"first_seq":193,"last_seq":732,"head_hash":"178fa854753632b9b4dc28f75d7c2d8ef854c29cc82bbb8eb48f812b1d45957c"}}
