在 PostgreSQL Gorm 中获取数据

发布于 2025-01-16 13:33:55 字数 2676 浏览 1 评论 0原文

我的模型如下:

package models

import "github.com/lib/pq"

type Guild struct {
    Id               string         `json:"id" gorm:"primaryKey"`
    DefaultBitrate   int            `json:"defaultBitrate"`
    DefaultState     string         `json:"defaultState"`
    DefaultCategory  string         `json:"defaultCategory"`
    DefaultUserLimit int            `json:"defaultUserLimit"`
    HelpChannel      string         `json:"helpChannel"`
    Generators       pq.StringArray `json:"generators" gorm:"type:text[]"`
    Channels         pq.StringArray `json:"channels" gorm:"type:text[]"`
}

函数文件:

func (h handler) CreateGuild(guildid string) error {
    guild := &models.Guild{
        Id:             guildid,
        DefaultBitrate: "64",
    }
    if result := h.DB.Create(&guild); result.Error != nil {
        return result.Error
    }
    return nil
}
func (h handler) GetGuild(guildid string) (models.Guild, error) {
    var guild models.Guild
    if result := h.DB.First(&guild, guildid); result.Error != nil {
        return guild, result.Error
    }
    return guild, nil
}

所以我要做的是首先创建一个公会,然后尝试使用相同的 id 获取它,但我没有在控制台

Database := db.Init()
    h := dbhandlers.New(Database)
    data, err := h.GetGuild("71728137382983743892")
    fmt.Print(data.DefaultBitrate)

Github 中记录任何内容: https://github.com/apidev234/abred

注意:我已经这样创建了公会:

 err := h.CreateGuild("71728137382983743892")

Debugs:

2022/03/24 13:37:23 /Users/gaurish/Desktop/Coding/TempVC-Bot/database/handlers/Functions.go:12 SLOW SQL >= 200ms
[1126.461ms] [rows:1] INSERT INTO "guilds" ("id","default_bitrate","default_state","default_category","default_user_limit","help_channel") VALUES ('ASDHA','64','','',0,'')
2022/03/24 13:37:44 /Users/gaurish/Desktop/Coding/TempVC-Bot/database/handlers/Functions.go:19 ERROR: column "asdha" does not exist (SQLSTATE 42703)
[229.439ms] [rows:0] SELECT * FROM "guilds" WHERE ASDHA ORDER BY "guilds"."id" LIMIT 1

Array:

func (h Handler) NewGenerator(guildid string, channelid string) {
    guild := models.Guild{
        Id: guildid,
    }
    if result := h.DB.First(&guild, "id = ?", guildid).Update("generators", append(guild.Generators, channelid)); result.Error != nil {
        return
    }
}

I Have my model as follows:

package models

import "github.com/lib/pq"

type Guild struct {
    Id               string         `json:"id" gorm:"primaryKey"`
    DefaultBitrate   int            `json:"defaultBitrate"`
    DefaultState     string         `json:"defaultState"`
    DefaultCategory  string         `json:"defaultCategory"`
    DefaultUserLimit int            `json:"defaultUserLimit"`
    HelpChannel      string         `json:"helpChannel"`
    Generators       pq.StringArray `json:"generators" gorm:"type:text[]"`
    Channels         pq.StringArray `json:"channels" gorm:"type:text[]"`
}

Functions File:

func (h handler) CreateGuild(guildid string) error {
    guild := &models.Guild{
        Id:             guildid,
        DefaultBitrate: "64",
    }
    if result := h.DB.Create(&guild); result.Error != nil {
        return result.Error
    }
    return nil
}
func (h handler) GetGuild(guildid string) (models.Guild, error) {
    var guild models.Guild
    if result := h.DB.First(&guild, guildid); result.Error != nil {
        return guild, result.Error
    }
    return guild, nil
}

So What i do is i create a guild first and then try to get it with the same id yet i don't get anything logged in the console

Database := db.Init()
    h := dbhandlers.New(Database)
    data, err := h.GetGuild("71728137382983743892")
    fmt.Print(data.DefaultBitrate)

Github: https://github.com/apidev234/abred

Note: I have already created the guild as such:

 err := h.CreateGuild("71728137382983743892")

Debugs:

2022/03/24 13:37:23 /Users/gaurish/Desktop/Coding/TempVC-Bot/database/handlers/Functions.go:12 SLOW SQL >= 200ms
[1126.461ms] [rows:1] INSERT INTO "guilds" ("id","default_bitrate","default_state","default_category","default_user_limit","help_channel") VALUES ('ASDHA','64','','',0,'')
2022/03/24 13:37:44 /Users/gaurish/Desktop/Coding/TempVC-Bot/database/handlers/Functions.go:19 ERROR: column "asdha" does not exist (SQLSTATE 42703)
[229.439ms] [rows:0] SELECT * FROM "guilds" WHERE ASDHA ORDER BY "guilds"."id" LIMIT 1

Array:

func (h Handler) NewGenerator(guildid string, channelid string) {
    guild := models.Guild{
        Id: guildid,
    }
    if result := h.DB.First(&guild, "id = ?", guildid).Update("generators", append(guild.Generators, channelid)); result.Error != nil {
        return
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2025-01-23 13:33:55

当将 First 与非数字主键一起使用时,您需要显式指定要与主键匹配的列。

官方文档

如果主键是字符串(例如,uuid),则查询
将写成如下:

db.First(&user, "id = ?", "1b74413f-f3b8-409f-ac47-e8c062e3472a")
// 从用户中选择 * WHERE id = "1b74413f-f3b8-409f-ac47-e8c062e3472a";

所以在GetGuild中 this:

h.DB.First(&guild, guildid)

应该是这样的:

h.DB.First(&guild, "id = ?", guildid)

When using First with non-number primary keys you need to explicitly specify the column against which you want to match the primary key.

Official docs:

If the primary key is a string (for example, like a uuid), the query
will be written as follows:

db.First(&user, "id = ?", "1b74413f-f3b8-409f-ac47-e8c062e3472a")
// SELECT * FROM users WHERE id = "1b74413f-f3b8-409f-ac47-e8c062e3472a";

So in GetGuild this:

h.DB.First(&guild, guildid)

should be this:

h.DB.First(&guild, "id = ?", guildid)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文