# 实例未绑定到会话；无法执行属性刷新操作

- **ID:** `python/sqlalchemy-detached-instance-session-closed`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在原始会话已关闭或提交后尝试访问或刷新 ORM 实例的属性，导致实例处于分离状态。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

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

## 无效尝试

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