go data_error ai_generated true

sql: Scan error on column index 1: converting driver.Value type string ("hello") to a int: invalid syntax

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

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.16 active
1.20 active

Root Cause

The database returned a value that cannot be converted to the target Go type during Scan, often due to schema mismatch.

generic

中文

数据库返回的值无法在 Scan 期间转换为目标 Go 类型,通常是由于架构不匹配。

Workarounds

  1. 85% success
    Scan into an intermediate type and convert: var tmp string; rows.Scan(&tmp); val, _ := strconv.Atoi(tmp)
  2. 90% success
    Use sql.NullInt64 to handle nullable/invalid values: var n sql.NullInt64; rows.Scan(&n); if n.Valid { val := n.Int64 }

Dead Ends

Common approaches that don't work:

  1. 60% fail

    May not be feasible in production or requires migration.

  2. 80% fail

    Leads to incorrect data handling and potential bugs.