2 回答
TA贡献1827条经验 获得超9个赞
上一篇:我跑了go get -u github.com/jinzhu/gorm
。我将使用sqlite
而不是pgsql
. 供参考,go version go1.12.7 linux/amd64
您的注释未使用正确的语法。gorm-属性-值对的分隔符不是
comma
,而是semi-colon
。https://github.com/jinzhu/gorm/blob/master/model_struct.go#L644的实现
Date.Scan()
必须使用指针接收器,否则您会丢失更新的值。https://github.com/jinzhu/gorm/blob/0fd395ab37aefd2d50854f0556a4311dccc6f45a/scaner_test.go#L57在我的测试期间,
Scan
方法收到一个time.Time
,因此不需要nil
大小写,也不string
需要大小写(可能是数据库驱动程序的问题)。然而,更重要的是,我添加了一个default
案例来捕获不受支持的类型。该
Scan
方法涉及将从数据库读取的数据分配给您提供的对象的过程。评论它与您在上次 sql 结果复制粘贴中演示的缺失日期问题无关。我认为这在某种程度上与我之前提到的问题有关。我听取了所有错误以确保一切按预期工作。
这是我的测试代码,
package main
import (
"database/sql/driver"
"encoding/json"
"fmt"
"log"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var DB *gorm.DB
type CustomerBroker struct {
gorm.Model
StartDate Date `gorm:"type:date;column:start_date" json:"start_date"`
EndDate Date `gorm:"type:date;column:end_date" json:"end_date"`
}
type Date struct {
time.Time
}
func (d *Date) UnmarshalJSON(b []byte) (err error) {
if b[0] == '"' && b[len(b)-1] == '"' {
b = b[1 : len(b)-1]
}
// take care of null..
if len(b) == 0 || string(b) == "null" {
d.Time = time.Time{}
return
}
d.Time, err = time.Parse("2006-01-02", string(b))
return
}
func (d *Date) Scan(b interface{}) (err error) {
switch x := b.(type) {
case time.Time:
d.Time = x
default:
err = fmt.Errorf("unsupported scan type %T", b)
}
return
}
func (d Date) Value() (driver.Value, error) {
// check if the date was not set..
if d.Time.IsZero() {
return nil, nil
}
return d.Time.Format("2006-01-02"), nil
}
func main() {
var dberr error
DB, dberr = gorm.Open("sqlite3", ":memory:")
if dberr != nil {
panic(dberr)
}
defer DB.Close()
record := CustomerBroker{}
errs := DB.CreateTable(record).GetErrors()
fmt.Println("create error ", errs)
data := []byte(`{"start_date": "2019-05-29", "end_date": null}`)
err := json.Unmarshal(data, &record)
fmt.Println("unmarshal error ", err)
log.Printf("record start %v end %v\n", record.StartDate, record.EndDate)
errs = DB.Create(&record).GetErrors()
fmt.Println("insert error ", errs)
all := []CustomerBroker{}
errs = DB.Find(&all).GetErrors()
fmt.Println("find error ", errs)
log.Printf("records count %v\n", len(all))
for _, a := range all {
log.Printf("found start %v end %v\n", a.StartDate, a.EndDate)
}
}
输出:
$ go run main.go
create error []
unmarshal error <nil>
2019/10/11 17:22:51 record start 2019-05-29 00:00:00 +0000 UTC end 0001-01-01 00:00:00 +0000 UTC
insert error []
find error []
2019/10/11 17:22:51 records count 1
2019/10/11 17:22:51 found start 2019-05-29 00:00:00 +0000 UTC end 0001-01-01 00:00:00 +0000 UTC
如果您在运行此命令时发现不同的行为pqsql,请共享相应的 docker 映像,以便将来可以针对同一版本进行尝试。
TA贡献1831条经验 获得超4个赞
启用DB.LogMode(true)
显示这两个查询
与扫描:
[2019-10-16 14:03:34] [0.97ms] INSERT INTO "custom_brokers" ("created_at","updated_at","deleted_at","start_date","end_date") VALUES ('2019-10-16 14:03:34','2019-10-16 14:03:34',NULL,'2019-05-29',NULL) RETURNING "custom_brokers"."id"
无需扫描:
[2019-10-16 14:02:53] [0.76ms] INSERT INTO "custom_brokers" ("created_at","updated_at","deleted_at") VALUES ('2019-10-16 14:02:53','2019-10-16 14:02:53',NULL) RETURNING "custom_brokers"."id"
第二个显示 gorm 完全忽略模型中的另一列(嵌入式 gorm 模型除外)
- 2 回答
- 0 关注
- 127 浏览
添加回答
举报