go
data_error
ai_generated
true
sql: no rows in result set
ID: go/sql-no-rows-in-result-set
80%Fix Rate
89%Confidence
0Evidence
2024-02-27First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
93% success
err := db.QueryRowContext(ctx, q, id).Scan(&u.Name) if errors.Is(err, sql.ErrNoRows) { return nil, ErrNotFound } return &u, err -
82% success
Use SELECT COUNT(*) first when existence matters, or return a typed ErrNotFound.
Dead Ends
Common approaches that don't work:
-
70% fail
Still returns zero rows; the error handling issue remains.
-
85% fail
Does not change the no-rows case.