I'm using https://github.com/go-pg/pg for my PostegreSQL database and Go. I'm trying to get the value of a table Settings
that has the column site_name
but for some reason it is giving me the error panic: model=Settings does not have primary keys
package mypackage
import (
"database"
)
type Settings struct {
SiteName string
}
func Get() string {
var name Settings
err := Db.Model(&name).First()
if err != nil {
panic(err)
}
return name.SiteName
}
I don't have the experience to really understand what's going on here or how to fix it. Any solutions? Thanks!
Example (FirstRow): https://godoc.org/github.com/go-pg/pg#DB.Select
The ORM requires you to define pks, so you will have to add it to the struct as well as your db table.
https://github.com/go-pg/pg/blob/master/orm/query.go#L558 https://github.com/go-pg/pg/blob/d4fc7afde3ee9d0a780f9c67daf8a8a3bfd84060/orm/table.go#L124
So add an Id
field to your struct
type Settings struct {
Id int64
SiteName string
}
If you want to use an ORM you have to play by it's rules :) If you want more control try using sqlx instead