Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
插入空值表示时间。时间从JSON解码_Json_Go_Mariadb_Go Gorm - Fatal编程技术网

插入空值表示时间。时间从JSON解码

插入空值表示时间。时间从JSON解码,json,go,mariadb,go-gorm,Json,Go,Mariadb,Go Gorm,我正在对现有生产数据库上的REST端点进行原型设计,但遇到了可为空的datetime列。我已经读过类似或的线程,但无法将新对象发布到REST端点,也无法将JSON中未列出的datetime类型属性作为NULL存储到MariaDB中 我正在提交一个HTTP请求,如 POST /countries { "iso_code" : "CZ", "title" : "Czechia" } 该表具有以下架构 创建表国家/地区 ( iso_代码字符(2)非空注释“国家alpha-2 iso代码” 主键, t

我正在对现有生产数据库上的REST端点进行原型设计,但遇到了可为空的datetime列。我已经读过类似或的线程,但无法将新对象发布到REST端点,也无法将JSON中未列出的datetime类型属性作为NULL存储到MariaDB中

我正在提交一个HTTP请求,如

POST /countries
{ "iso_code" : "CZ", "title" : "Czechia" }
该表具有以下架构

创建表国家/地区
(
iso_代码字符(2)非空注释“国家alpha-2 iso代码”
主键,
title varchar(255)空注释“国家名称”,
已在datetime上创建\u默认当前\u时间戳()不为空,
更新当前时间戳()时更新的时间戳为空
)
我已经尝试了mysql.NullTime类型,time.time类型作为指针,在映射中指定sql:DEFAULT:NULL,gorm:“NULL”映射,在保存之前,我还直接将nil分配到newCountry.UpdatedAt。在拯救戈姆之前,胡克也没什么不同

模式非常明确

类型国家/地区结构{
IsoCode字符串`json:“iso_代码”gorm:“主键”sql:“大小:2”`
Title*string`json:“Title,ommitempty”gorm:“null”sql:“size:255”`
CreatedAt*time.time`json:“创建时间”,省略为空“gorm:”列:创建时间”`
UpdatedAt*time.time`json:“updated_at,ommitempty”gorm:“column:updated_on;null”sql:“DEFAULT:null”`
}
我正在使用GORM存储对象

func CreateCountry(country *m.Country) *m.Country {
    fmt.Println(country)

    existingCountry := GetCountry(country.IsoCode)
    if existingCountry == nil {
        err := GetDB().Create(country).Error
        // ...
    } 
    // ...
}
这里,在方法中,country没有
UpdatedAt
的值,但是调用
Create(country)
,它将精确的值存储到两个datetime列

我是这样从HTTP请求体读取的

var CreateCountry = func(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.Body)

    newCountry := &m.Country{}
    err := json.NewDecoder(r.Body).Decode(newCountry)
    // ...
}
在这里,body的datetime属性为nil

不确定还能做些什么来生成insert语句,如

插入'country'('iso_代码'),
`标题“,”已创建“,”已更新“)值(?,,,?)[KR 0xc0001f6130 null]
而不是

插入'country'('iso_代码'),
`标题“,”已创建“,”已更新“)值(?,,,?)[KR 0xc0001f6130 2019-03-31 03:16:41.5158848+0200 CEST m=+16.4487208
01 2019-03-31 03:16:41.5158848+0200 CEST m=+16.448720801]

如果您在
[KR 0xc0001f6130 null null]
中说
nil
而不是
null
怎么办?如果您在
[KR 0xc0001f6130 null null]
中说
nil
而不是
null
怎么办?