问题描述

在爬取数据的时候,遇到需要存储的数据,正常情况下,在使用gorm进行结构体迁移建表的时候,对于结构体内部有切片类型,不能够迁移成功,因为无法识别切片类型。

这样是不符合的,正确的做法是重新定义一个标签,标明其的类型

然后需要对Work重新实现Scan和Value方法,也就是把其分成字节读取然后重新合成一个string类型存储到数据库中

示例:

type Alibaba1 struct {
	Uuid          int    `gorm:"primaryKey;column:uuid"`
	ID            int    `json:"id" column:"id"`
	Company       string `gorm:"column:company"`                   // 公司id
	Title         string ` gorm:"column:title"`                    //工作名字
	Job_category  string `gorm:"column:job_category"`              //工作类型
	Job_type_name string ` gorm:"column:job_type_name"`            //工作种类
	Job_detail    string ` gorm:"column:job_detail;type:longtext"` //工作职责
	WorkLocation  Work   `gorm:"column:job_location"`
	Fetch_time    string `gorm:"column:fetch_time"`
}
type Work []string

func (t *Work) Scan(value interface{}) error {
	bytesValue, _ := value.([]byte)
	return json.Unmarshal(bytesValue, t)
}
func (t Work) Value() (driver.Value, error) {
	return json.Marshal(t)
}

结果: