# sqlalchemy.orm.exc.DetachedInstanceError: Instance <User at 0x...> is not bound to a Session

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

## Root Cause

Attempting to access or modify an ORM instance that has been detached from its session, usually after session.close() or session.expunge_all().

## Version Compatibility

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

## Workarounds

1. **Use session.merge() to reattach the instance** (95% success)
   ```
   merged_user = session.merge(detached_user)
print(merged_user.name)
   ```
2. **Refresh the instance with session.refresh() after reattaching** (85% success)
   ```
   session.add(detached_user)
session.refresh(detached_user)
   ```

## Dead Ends

- **Calling session.add() on the detached instance** — The instance is not in a pending state; it needs to be merged first. (80% fail)
- **Reopening the session and reusing the same instance** — The instance's state is stale; you need to merge or refresh it. (70% fail)
