go
data_error
ai_generated
true
sql: Scan error on column index 2: converting NULL to string is unsupported
ID: go/database-sql-null-value
80%Fix Rate
90%Confidence
0Evidence
2024-02-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.18 | active | — | — | — |
| 1.19 | active | — | — | — |
Root Cause
Scanning a NULL database column into a non-nullable Go type (e.g., string) without using sql.NullString.
generic中文
将数据库中的NULL列扫描到不可为空的Go类型(如string)中,而未使用sql.NullString。
Workarounds
-
95% success
var s sql.NullString if err := rows.Scan(&s); err != nil { // handle error } if s.Valid { fmt.Println(s.String) } -
80% success
Use COALESCE in SQL query to convert NULL to default value.
Dead Ends
Common approaches that don't work:
-
70% fail
NULL is not the same as an empty string and can lead to data loss.
-
90% fail
Go does not allow direct casting of NULL to string.