go
data_error
ai_generated
true
sql: Scan error on column index 2: unsupported Scan, storing driver.Value type <nil> into type *time.Time
ID: go/unsupported-scan-type
90%Fix Rate
85%Confidence
1Evidence
2023-11-08First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| go1.21 | active | — | — | — |
| go1.22 | active | — | — | — |
| go1.23 | active | — | — | — |
Root Cause
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.
generic中文
数据库为映射到非指针 Go 类型(如 time.Time)的列返回了 NULL 值,该类型无法表示 nil。
Official Documentation
https://pkg.go.dev/database/sql#NullTimeWorkarounds
-
95% success Use a pointer type for the Go field, e.g., *time.Time instead of time.Time, to allow nil values.
Use a pointer type for the Go field, e.g., *time.Time instead of time.Time, to allow nil values.
-
90% success Use sql.NullTime type from the database/sql package to handle nullable timestamps.
Use sql.NullTime type from the database/sql package to handle nullable timestamps.
中文步骤
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.
Dead Ends
Common approaches that don't work:
-
Changing the column type in the database to NOT NULL.
50% fail
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% fail
COALESCE changes the query result but may mask missing data; also requires modifying every query.