go data_error ai_generated true

sql: no rows in result set

ID: go/sql-no-rows-in-result-set

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-02-27First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —

Root Cause

QueryRow.Scan found no matching row. Code treats sql.ErrNoRows as a fatal error instead of a normal not-found case.

generic

中文

QueryRow.Scan 未找到匹配行。代码将 sql.ErrNoRows 当作致命错误而非正常的未找到情形。

Workarounds

  1. 93% success
    err := db.QueryRowContext(ctx, q, id).Scan(&u.Name)
    if errors.Is(err, sql.ErrNoRows) {
      return nil, ErrNotFound
    }
    return &u, err
  2. 82% success
    Use SELECT COUNT(*) first when existence matters, or return a typed ErrNotFound.

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Still returns zero rows; the error handling issue remains.

  2. 85% fail

    Does not change the no-rows case.