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}
换成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")
Most helpful comment
"root:root@/gin?charset=utf8"改為"root:root@/gin?charset=utf8&parseTime=True"試試看?