# sqlalchemy.orm.exc.DetachedInstanceError: Instance <User at 0x...> is not bound to a Session; lazy load operation of attribute 'posts' cannot proceed

- **ID:** `python/sqlalchemy-lazy-load-outside-session`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Trying to access a lazy-loaded relationship after the session has been closed or the object has been detached.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.4.x | active | — | — |
| 2.0.x | active | — | — |

## Workarounds

1. **** (95% success)
   ```
   Eagerly load relationships before closing the session: user = session.query(User).options(joinedload(User.posts)).first()
   ```
2. **** (80% success)
   ```
   Use session.expire_on_commit=False to keep objects usable after commit.
   ```

## Dead Ends

- **** — Adding a detached object may create duplicate or raise errors; it doesn't restore the original session context. (70% fail)
- **** — Merge works but may overwrite changes; also it's not ideal for read-only lazy loads. (40% fail)
