# sqlalchemy.orm.exc.DetachedInstanceError: Instance is not bound to a Session; attribute refresh operation cannot proceed

- **ID:** `python/sqlalchemy-detached-instance-session-closed`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Attempting to access or refresh attributes of an ORM instance after its originating session has been closed or committed, leaving the instance in a detached state.

## Version Compatibility

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

## Workarounds

1. **Merge the detached instance back into the session** (85% success)
   ```
   Use session.merge() to re-attach the detached instance to a new session before accessing attributes.
   ```
2. **Refresh the instance before session closure** (90% success)
   ```
   Ensure the session remains open and use session.refresh() before closing, or load needed attributes eagerly.
   ```

## Dead Ends

- **Re-querying the object with session.query() without merging** —  (70% fail)
- **Enabling autocommit on the session** —  (60% fail)
