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

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2023-11-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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#NullTime

Workarounds

  1. 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.
  2. 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.

中文步骤

  1. Use a pointer type for the Go field, e.g., *time.Time instead of time.Time, to allow nil values.
  2. Use sql.NullTime type from the database/sql package to handle nullable timestamps.

Dead Ends

Common approaches that don't work:

  1. 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.

  2. 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.