Gorm: 加入created_at等时间字段 数据除了ID其他field就变为空了

Created on 18 Jan 2017  ·  5Comments  ·  Source: go-gorm/gorm

package main

import (
    "fmt"
    "time"

    _ "github.com/go-sql-driver/mysql"
    "github.com/jinzhu/gorm"
)

// Product model
type Product struct {
    ID        uint
    CreatedAt time.Time
    Name      string
}

type Person struct {
    ID   uint
    Name string
}
func main() {
    db, _ := gorm.Open("mysql", "root:root@/gin?charset=utf8")
    defer db.Close()
    db.DropTable(&Product{}, &Person{})
    db.AutoMigrate(&Product{}, &Person{})

    // Product
    product := Product{Name: "Product one"}
    db.Create(&product)
    p1 := Product{}
    db.Last(&p1)
    fmt.Println(p1)

    // Person
    person := Person{Name: "Person one"}
    db.Create(&person)
    p2 := Person{}
    db.Last(&p2)
    fmt.Println(p2)
}

Result:

{1 0001-01-01 00:00:00 +0000 UTC }

(sql: Scan error on column index 1: unsupported Scan, storing driver.Value type []uint8 into type *time.Time
)
[2017-01-18 22:24:04]
{1 Person one}

Most helpful comment

"root:root@/gin?charset=utf8" 改為 "root:root@/gin?charset=utf8&parseTime=True" 試試看?

All 5 comments

换成sqlite的驱动和数据库没问题了

"root:root@/gin?charset=utf8" 改為 "root:root@/gin?charset=utf8&parseTime=True" 試試看?

Thanks @YamiOdymel, I had the same problem.

@YamiOdymel Thanks!

db, _ := gorm.Open("mysql", "root:root@/gin?charset=utf8")

换成

db, _ := gorm.Open("mysql", "root:root@/gin?charset=utf8&parseTime=True&loc=Local")

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sredxny picture sredxny  ·  3Comments

kumarsiva07 picture kumarsiva07  ·  3Comments

Quentin-M picture Quentin-M  ·  3Comments

pjebs picture pjebs  ·  3Comments

bramp picture bramp  ·  3Comments