python
data_error
ai_generated
true
sqlalchemy.exc.MultipleResultsFound: 需要恰好一行时找到多行
sqlalchemy.exc.MultipleResultsFound: Multiple rows were found when exactly one was required
ID: python/flask-sqlalchemy-multiple-results-found
80%修复率
86%置信度
0证据数
2024-09-08首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
对返回多行的查询使用了 query.one() 或 scalar_one(),通常由于缺少唯一约束或存在重复数据。
English
query.one() or scalar_one() used on a query that returns multiple rows, often due to missing unique constraint or duplicate data.
解决方案
-
88% 成功率
Use .one_or_none() and handle None explicitly
-
92% 成功率
Add unique constraint: email = db.Column(db.String, unique=True)
无效尝试
常见但无效的做法:
-
50% 失败
Silently returns arbitrary row; hides data bug.
-
55% 失败
Same silent-truncation risk.