go data_error ai_generated true

sql:结果集中没有行

sql: no rows in result set

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

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2024-02-27首次发现

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.21 active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 70% 失败

    Still returns zero rows; the error handling issue remains.

  2. 85% 失败

    Does not change the no-rows case.