go data_error ai_generated true

sql: Scan error on column index 2, name "email": converting NULL to string is unsupported

ID: go/sql-converting-null-to-string

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —

Root Cause

A nullable column was scanned into a non-pointer string. NULL cannot be assigned to string.

generic

中文

向非指针 string 扫描可空列。NULL 无法赋给 string。

Workarounds

  1. 92% success
    var email sql.NullString
    err := row.Scan(&email)
    if email.Valid { u.Email = email.String }
  2. 88% success
    Use a pointer *string for nullable columns.

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Loses the distinction between empty and NULL.

  2. 65% fail

    Requires type assertion and is error-prone.