go data_error ai_generated true

sql: Scan error on column index 0: converting NULL to string is unsupported

ID: go/sql-scan-into-nil-pointer

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-03-25First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.21 active

Root Cause

Database returned NULL for a column scanned into a non-nullable Go type.

generic

中文

数据库为某个列返回 NULL,但扫描到了不可为空的 Go 类型。

Workarounds

  1. 95% success
    Use sql.NullString: var ns sql.NullString; rows.Scan(&ns); if ns.Valid { use ns.String }
  2. 90% success
    Use pointers: var s *string; rows.Scan(&s); if s != nil { use *s }

Dead Ends

Common approaches that don't work:

  1. 70% fail

    May cause incorrect logic; NULL vs empty differ.

  2. 40% fail

    May not be possible; data already has NULLs.