登录
首页 >  Golang >  Go问答

Golang Gorm 使用切片和 postgres 的 jsob 字段

来源:stackoverflow

时间:2024-04-10 11:54:34 169浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《Golang Gorm 使用切片和 postgres 的 jsob 字段》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

我需要保存 [] 或具有不同整数值(如 [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)
        //      // }
        //  }
        // }

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

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

我遇到的一些错误:

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

解决方案


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

您无法在 *bytes.Buffer范围。您可以使用 Buffer.Bytes() 方法来访问该类型的字节。查看this example

根据错误判断,我猜测当您在扫描 DateOfMonth 时使用类型 []int64 时会发生这种情况。出现此错误的可能性之一是您的数据库将值存储为 []uint8 而不是 []int64

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

今天关于《Golang Gorm 使用切片和 postgres 的 jsob 字段》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>