# 对象 '<User at 0x...>' 正在被合并到会话中，但相同的实例已存在

- **ID:** `python/sqlalchemy-orm-exc-concurrent-modification-error`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

尝试合并一个已被同一会话跟踪的 ORM 实例，导致冲突。

## 版本兼容性

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

## 解决方案

1. **Check session identity map** (80% 成功率)
   ```
   Check if the instance is already in the session using session.is_modified() or track identities manually before merging.
   ```
2. **Merge with fresh instance** (90% 成功率)
   ```
   Use session.merge() with a new instance that has the same primary key, ensuring the session does not already contain it.
   ```

## 无效尝试

- **Using add() instead of merge()** —  (70% 失败率)
- **Expunging the instance first** —  (50% 失败率)
