go - 无法更新 jsonb(即子数组)列
问题描述
我刚刚开始使用 go-pg,并且在使用 PostgreSQL DB 的 JSONB 功能时遇到了一些问题。
我已将我的模型结构定义为:
type Chemical struct {
Id int
ChemicalName string
ListingDetails []ListingDetail `sql:",array"`
ParentChemical *Chemical `pg:"fk_parent_chemical_id"`
ParentChemicalId int
}
type ListingDetail struct {
TypeOfToxicity string
ListingMechanism string
CasNo string
ListedDate time.Time
SafeHarborInfo string
}
在 Chemical 结构中 - 通过使用sql:",array"
ListingDetails 字段上的标签 - 我的理解是这应该映射到目标表中的 jsonb 列。使用 go-pg 的 CreateTable - 使用 listing_details jsonb 列创建化学品表 - 所以在这方面一切都很好。
但是,我似乎无法成功更新此字段(在数据库中)。当我最初在数据库中创建化学记录时 - 没有 ListingInfos - 我稍后会回去更新它们。下面是一个片段,展示了我是如何做到这一点的。
detail := models.ListingDetail{}
detail.TypeOfToxicity = strings.TrimSpace(row[1])
detail.ListingMechanism = strings.TrimSpace(row[2])
detail.CasNo = strings.TrimSpace(row[3])
detail.SafeHarborInfo = strings.TrimSpace(row[5])
chemical.ListingDetails = append(chemical.ListingDetails, detail)
err = configuration.Database.Update(&chemical)
if err != nil {
panic(err)
}
但我总是收到如下所示的错误消息。我究竟做错了什么??
panic: ERROR #22P02 malformed array literal: "{{"TypeOfToxicity":"cancer","ListingMechanism":"AB","CasNo":"26148-68-5","ListedDate":"1990-01-01T00:00:00Z","SafeHarborInfo":"2"}}"
解决方案
推荐阅读
- javascript - ReactJS:如何在 JavaScript 中过滤数组以在地图上显示特定图像
- c# - Azure 功能未“等待”任务完成
- javascript - 用户注册或登录时是否应该创建 Token?
- flutter - 使用 canvas.drawVertices 更改顶点和线条的颜色
- javascript - 以离子和角度将变量从 .ts 传递到 .html
- list - 试图找到一种在powershell中列出两个日期之间的所有星期五日期的方法
- python - 如何从 django 中的表中检索 comment_replies
- python - 如何根据两个熊猫数据框参数的当前值控制在函数中应用哪个公式?
- java - 未找到 maven-compiler-plugin
- javascript - 使用 JavaScript 访问 localStorage 数据