go
data_error
ai_generated
true
sql:列索引 2 上的 Scan 错误:不支持的 Scan,将 driver.Value 类型 <nil> 存储到 *time.Time 类型中
sql: Scan error on column index 2: unsupported Scan, storing driver.Value type <nil> into type *time.Time
ID: go/unsupported-scan-type
90%修复率
85%置信度
1证据数
2023-11-08首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| go1.21 | active | — | — | — |
| go1.22 | active | — | — | — |
| go1.23 | active | — | — | — |
根因分析
数据库为映射到非指针 Go 类型(如 time.Time)的列返回了 NULL 值,该类型无法表示 nil。
English
The database returned a NULL value for a column that is mapped to a non-pointer Go type like time.Time, which cannot represent nil.
官方文档
https://pkg.go.dev/database/sql#NullTime解决方案
-
Use a pointer type for the Go field, e.g., *time.Time instead of time.Time, to allow nil values.
-
Use sql.NullTime type from the database/sql package to handle nullable timestamps.
无效尝试
常见但无效的做法:
-
Changing the column type in the database to NOT NULL.
50% 失败
This may break existing data or require schema migration; also not always under developer control.
-
Using a default value in the SQL query with COALESCE.
40% 失败
COALESCE changes the query result but may mask missing data; also requires modifying every query.