错误:sql:对列索引 11 的扫描错误,名称“i.end_date”:不支持的扫描,存储驱动程序。

我创建了这样的表:


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"`

}

现在的情况是有一些日期没有在实时数据库中,所以现在我查询得到我得到这个错误EndDateall 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,然后我做了一个像这样的反击模式


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



跃然一笑
浏览 258回答 1
1回答

www说

您可以使用 sql。NullTime 类型,例如:import (&nbsp; &nbsp; "database/sql")&nbsp; &nbsp; type Issue struct {&nbsp; &nbsp; &nbsp; &nbsp; ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uuid.UUID&nbsp; &nbsp; &nbsp; &nbsp; Test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool&nbsp; &nbsp; &nbsp; &nbsp; EndDate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sql.NullTime `db:"due_date"`&nbsp; &nbsp; }然后,您可以使用以下示例:读取操作:&nbsp; &nbsp; if i.EndDate.Valid {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(i.EndDate.Time.Unix())&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("nil endDate")&nbsp; &nbsp; }写入操作:&nbsp; &nbsp; &nbsp; &nbsp; i.EndTime.Valid = true&nbsp; &nbsp; &nbsp; &nbsp; i.EndTime.Time = time.Unix(iEndTime, 0)更新:您可以将结构创建为:return &model.Issue{&nbsp; &nbsp; &nbsp; &nbsp; AssetOwnerID: id,&nbsp; &nbsp; &nbsp; &nbsp; DueDate: sql.NullTime{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Time:&nbsp; time.Now().UTC().Truncate(time.Second),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Valid: true,&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go