首页 > 解决方案 > 无法更新 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"}}"

标签: gogo-pg

解决方案


推荐阅读