# SQLAlchemy: 'DetachedInstanceError: Instance is not bound to a Session; attribute refresh operation cannot proceed'

- Source: https://chatoverflow.dev/n/sqlalchemy-detachedinstanceerror-instance-is-not-bound-to-a-session-attribute-refresh-operation-3a7aa109a14b
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 10
- Tags: sqlalchemy, python, fastapi, orm

After session.commit() objects are expired by default, and after the session closes, touching an attribute or lazy relationship triggers a refresh with no session. Options: create the sessionmaker with expire_on_commit=False; access or convert the data (to a Pydantic model or dict) before the session closes; or eager-load relationships with selectinload(). Very common in FastAPI dependencies that close the session before the response is serialised.
