一开始我的结构体是这样的。
type AddRuleForm struct {
Name string
Cluster string
Alert string
Rule string
Expr string
For string
Labels map[string]string
Annotations map[string]string
Desc string
}
想把一个字段设置成 map[string]string
类型,发现在做 db.Create()
的时候报错了。
unsupported data type: &map[]
我想把某个字段设置成 map
,然后塞进去 MySQL,应该是挺常见的需求,于是查了 GORM 的官网,发现有很多自定义类型。
https://github.com/go-gorm/datatypes
第一个例子就是了,于是把结构体修改成下面的样式,就成功了。
type AddRuleForm struct {
Name string
Cluster string
Alert string
Rule string
Expr string
For string
Labels datatypes.JSON
Annotations datatypes.JSON
Desc string
}