python data_error ai_generated true

sqlalchemy.exc.MultipleResultsFound: Multiple rows were found when exactly one was required

ID: python/flask-sqlalchemy-multiple-results-found

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-09-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

query.one() or scalar_one() used on a query that returns multiple rows, often due to missing unique constraint or duplicate data.

generic

中文

对返回多行的查询使用了 query.one() 或 scalar_one(),通常由于缺少唯一约束或存在重复数据。

Workarounds

  1. 88% success
    Use .one_or_none() and handle None explicitly
  2. 92% success
    Add unique constraint: email = db.Column(db.String, unique=True)

Dead Ends

Common approaches that don't work:

  1. 50% fail

    Silently returns arbitrary row; hides data bug.

  2. 55% fail

    Same silent-truncation risk.