ERROR: sql: Scan error on column index 11, name "i.end_date": unsupported Scan, storing driver.Value type <nil> into type *time.Time

ERROR: sql: Scan error on column index 11, name "i.end_date": unsupported Scan, storing driver.Value type <nil> into type *time.Time

我这样创建了 table :

CREATE TABLE MyTable
(
    id                  uuid,
    Test                BOOLEAN   NOT NULL,
    end_date            TIMESTAMP NULL DEFAULT NULL,
    PRIMARY KEY (id)
);

我的结构

type Issue struct {
    ID                uuid.UUID
    Test              bool
    EndDate           time.Time `db:"due_date"`
}

现在的情况是 实时数据库 中有某个日期没有 EndDate 所以现在我在哪里查询 all data 我收到这个错误

ERROR: sql: Scan error on column index 11, name "i.end_date": unsupported Scan, storing driver.Value type <nil> into type *time.Time

我不知道问题出在哪里。

更新

如果我使用sql.NullTime然后我做了一个像这样的respoce模式

return &model.Issue{
        AssetOwnerID: id,
        DueDate :       time.Now().UTC().Truncate(time.Second)
    } 

我遇到了这个错误

 Cannot use 'time.Now().UTC().Truncate(time.Second)' (type Time) as type sql.NullTime

您可以使用 sql.NullTime 类型,例如:

import (
    "database/sql"
)


    type Issue struct {
        ID                uuid.UUID
        Test              bool
        EndDate           sql.NullTime `db:"due_date"`
    }

那么,你可以举个例子:

读取操作:

    if i.EndDate.Valid {
        fmt.Println(i.EndDate.Time.Unix())
    } else {
        fmt.Println("nil endDate")
    }

写操作:

        i.EndTime.Valid = true
        i.EndTime.Time = time.Unix(iEndTime, 0)

更新:

您可以将结构创建为:

return &model.Issue{
        AssetOwnerID: id,
        DueDate: sql.NullTime{
            Time:  time.Now().UTC().Truncate(time.Second),
            Valid: true,
        }