0

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
    }
}
7
  • Your CreateGuild method is not using the given guildid in any way. How is gorm supposed to know the id that you want to assign to the created Guild? Commented Mar 24, 2022 at 8:03
  • My model does that Commented Mar 24, 2022 at 8:05
  • Include that code in the question. Commented Mar 24, 2022 at 8:06
  • The github link has the model, Also i updated my createGuild function check that once... Even the question has the model.. the very first codeblock Commented Mar 24, 2022 at 8:08
  • Also you can turn on debugging to see the executed SQL queries, it should show you the INSERT query generated for the Create(&guild) call and you should see if the guildid is being used or not. Commented Mar 24, 2022 at 8:09

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

9 Comments

I believe you just need to turn of the Debug mode
Alright.. Probably doing silent mode would help
@Gaurish Or you can directly change the logger's log level to a db.Logger.LogMode(logger.Silent)
Yeah.. Could also be done while initalizing throught gorm's config...
@Gaurish for such types, as far as I know, you need to use declared types that implement the sql.Scanner and driver.Valuer interfaces and those implementation transform the Go type from/to the PostgreSQL type. For an example you can take a look at lib/pq.StringArray (you can change []string in your Guild to be pq.StringArray if you don't want to write your own implementations).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.