# sqlalchemy.exc.MultipleResultsFound: 当恰好需要一行时，找到了多行

- **ID:** `python/sqlalchemy-query-multiple-rows`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

对返回多行的查询使用了 .one()。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Use .scalar_one() if you expect exactly one row, or .one_or_none() if zero is acceptable.
   ```
2. **** (95% 成功率)
   ```
   Add filters to narrow down the result: query.filter(User.id == some_id).one()
   ```

## 无效尝试

- **** — Silently ignores extra rows, possibly hiding data issues. (60% 失败率)
- **** — Doesn't solve the root cause; may still return the wrong row. (70% 失败率)
