【问题标题】:Golang Gorm working with slices and postgres' jsob fieldGolang Gorm 使用 slices 和 postgres 的 jsob 字段
【发布时间】:2020-11-26 15:00:53
【问题描述】:

我需要保存 [] 或具有不同整数值的列表,例如 [1, 7, 8]。这些值可以是 1-31 之间的任何值。

我的这个字段 (DateOfMonth) 的结构是:

type Subscription struct {
    gorm.Model
    Enabled            bool    `gorm:"DEFAULT:True"`
    Deleted            bool    `gorm:"DEFAULT:False"`
    UserID             uint    `gorm:"not null"`
    Cap                int     `gorm:"DEFAULT:-1"`
    DateOfMonth        []int64 `gorm:"type:json default '[]'::json"`
}

现在,我需要在 API 中读取此值并将其与 current_date 进行比较。

为此,我已经尝试过:

type Result struct {
        ID               uint
        Email            string
        UniqueIdentifier string
        Cap              int
        DateOfMonth      []uint8
    }
    var subscriptions []Result
    if err := db.Table("users").Select("users.id, users.email, users.unique_identifier, subscriptions.cap, subscriptions.date_of_month").Joins("join subscriptions on users.id = subscriptions.user_id").Where("subscriptions.subscription_type_id=? and users.is_verified=? and subscriptions.enabled=?", subscription_type_id, true, true).Find(&subscriptions).Error; err != nil {
        c.JSON(http.StatusNotFound, gin.H{"error": true, "reason": "Subscribers not found!", "code": http.StatusBadRequest, "status": "failure"})
        return
    }

如果我将DateOfMonth []uint8 更改为DateOfMonth []int64,则会出错。 我在此字段中收到的值是字节值列表 例如,[] -> [91 93] 和 [6] -> [91 54 93]

如果我这样做了,bytes.NewBuffer(s.DateOfMonth),我会得到正确的值,但是我需要遍历这个切片以将它与今天的日期进行比较。我尝试了很多方法来获取循环中的实际值(6)(dom值),但无济于事。

// if len(s.DateOfMonth) > 0 {
        //  // date_of_month_new := binary.BigEndian.Uint64(date_of_month)
        //  todays_date_of_month := time.Now().Day()
        //  fmt.Println(todays_date_of_month) //, date_of_month, reflect.TypeOf(date_of_month))
        //  for _, dom := range s.DateOfMonth {
        //      fmt.Println("help", reflect.TypeOf(dom), dom, todays_date_of_month)
        //      // if dom == todays_date_of_month {
        //      //  fmt.Println("matching", dom, todays_date_of_month)
        //      // }
        //  }
        // }

我什至尝试过各种答案的建议,例如thisthisthis

我在这里缺少什么?我们将非常感谢您的帮助。

我得到的一些错误:

invalid sql type DateOfMonth (slice) for postgres
Golang cannot range over pointer to slice
cannot range over bytes.NewBuffer(s.DateOfMonth) (type *bytes.Buffer)
sql: Scan error on column index 4, name "date_of_month": unsupported Scan, storing driver.Value type []uint8 into type *[]int 

【问题讨论】:

  • 请提及您遇到的错误
  • @VaibhavMishra 添加了错误
  • 为什么不使用数组类型而不是 JSON?这个问题可能更容易解决
  • postgresql 不支持数组类型,我不想与该字段建立关系。我只需要保存一些整数值进行处理
  • 我很抱歉@AnkitaGupta,但如果您能将错误添加到他们的堆栈跟踪或触发它们的代码中,那就太好了。您在问题中提到的注释掉的代码也不清楚。它是什么,它的目的是什么?为什么要评论?另外,您如何准确尝试提到的建议及其结果?我正在添加一个答案,我现在对你的问题有什么理解,希望它有所帮助。

标签: json postgresql go go-gorm


【解决方案1】:
Golang cannot range over pointer to slice

您正在迭代指向切片的指针,而不是切片。这意味着您必须首先取消引用您的变量,然后对其进行循环。查看this example

cannot range over bytes.NewBuffer(s.DateOfMonth) (type *bytes.Buffer)

你不能范围超过类型*bytes.Buffer。您可以改为使用方法Buffer.Bytes() 访问该类型的字节。查看this example

sql: Scan error on column index 4, name "date_of_month": unsupported Scan, storing driver.Value type []uint8 into type *[]int

从错误来看,我猜这是在扫描DateOfMonth 时使用类型[]int64 时发生的。此错误的一种可能性是您的数据库将值存储为[]uint8 而不是[]int64

postgres 的无效 sql 类型 DateOfMonth(切片)

我会在成功重现此错误后尝试更新我的答案。

【讨论】:

    猜你喜欢
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多