I'm using go-gorm to fetch values from a PostgreSQL database. The relevant code is shown below:
type Chip struct {
UUID string `db:uuid`
URL string `db:url`
N int `db:n`
Pack_ID int `db:pack_id`
}
func getChip(uuid string) (Chip, error) {
var c Chip
err := DB.Model(Chip{}).Where("uuid = ?", uuid).First(&c)
return c, err.Error
}
When I pass a UUID string to getChip
, the correct row is returned and all values are correct except for c.Pack_ID
, which is always 0
. Incidentally there is never a row for which Pack_ID
is 0
.
Here is a screenshot from pgAdminIII, which I hope will shed some light on the problem:
Any ideas as to what might be going wrong? I'm at a complete loss, here...
The struct tags you're using appear to be malformed. A struct tag should be of the form:
name:"value"
but what you've got is missing the quotes around the value:
name:value
Try correcting this. Otherwise, the struct tag parser in Go has no chance to work, as it depends on those quotes, as seen in the struct tag parser implementation.
With regards to specific struct tags: are you sure you're supposed to use db
? According to the Gorm documentation, you probably want to use gorm:"column:..."
. I was expecting your type definition to be:
type Chip struct {
UUID string `gorm:"column:uuid;primary_key"`
URL string `gorm:"column:url"`
N int `gorm:"column:n"`
Pack_ID int `gorm:"column:pack_id"`
}