# sqlalchemy.exc.MultipleResultsFound: 对于 one() 找到了多行

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

## 根因

对返回多行的查询使用 one() 方法，而该方法期望只返回一行。

## 版本兼容性

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

## 解决方案

1. **Use one_or_none() to handle zero or one result** (95% 成功率)
   ```
   user = session.query(User).filter_by(id=user_id).one_or_none()
if user is None:
    # Handle not found
    pass
   ```
2. **Use scalar() for single column queries** (90% 成功率)
   ```
   count = session.query(func.count(User.id)).scalar()
   ```

## 无效尝试

- **Using first() instead without checking** — first() returns None if no rows, masking potential logic errors. (60% 失败率)
- **Ignoring the error and assuming one row** — The error will still occur. (100% 失败率)
