go data_error ai_generated true

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

ID: go/database-sql-null-value-in-non-null-column

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

Scanning a NULL database value into a non-nullable Go type, such as string or int, without using sql.Null* types.

generic

中文

将NULL数据库值扫描到不可为空的Go类型,如string或int,而未使用sql.Null*类型。

Workarounds

  1. 95% success Use sql.NullString and similar types
    var name sql.NullString
    if err := rows.Scan(&name); err != nil { return err }
    if name.Valid { /* use name.String */ } else { /* handle NULL */ }

Dead Ends

Common approaches that don't work:

  1. Using default values manually 50% fail

    May mask actual NULL meaning; use NullString for proper handling.