go type_error ai_generated true

error: sql: Scan error on column index 0: unsupported Scan, storing driver.Value type int64 into type *string

ID: go/database-sql-rows-scan-type-mismatch

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

Scanning a database column into a Go variable of incompatible type, e.g., scanning an integer into a string pointer.

generic

中文

将数据库列扫描到不兼容类型的Go变量中,例如将整数扫描到字符串指针。

Workarounds

  1. 90% success Use sql.NullString or appropriate types
    var name sql.NullString
    if err := rows.Scan(&name); err != nil { return err }
    if name.Valid { /* use name.String */ }

Dead Ends

Common approaches that don't work:

  1. Using fmt.Sprintf to convert values manually 60% fail

    Inefficient and error-prone; use proper type mapping.