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

- **ID:** `python/flask-sqlalchemy-detachedinstanceerror`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Accessing an attribute of an ORM object after the session that loaded it has been closed or committed.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 2.x | active | — | — |
| 3.x | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   user = session.query(User).options(joinedload(User.posts)).first()
# use user.posts before session.close()
   ```
2. **** (85% success)
   ```
   Use `db.session` within the request context; avoid manual session.close().
   ```

## Dead Ends

- **** — This only prevents expiration on commit, not detachment after session close. (60% fail)
- **** — The session is already closed, so refresh fails. (80% fail)
