pg
pg copied to clipboard
Warning 'tag name "X" is also an option name; is it a mistake?'
After migration to v10
I started to notice such warnings in the logs: tag name "type" is also an option name; is it a mistake?
I found out there's a check whether tag name is equal to one of the available field options. I am sure most of them won't be used as a column name, but some (like type
) can appear in a real-life project.
For example, some kind of logging
package main
import (
"context"
"github.com/go-pg/pg/v10"
)
type Log struct {
ID int `pg:"id,pk"`
Type LogType `pg:"type"`
Msg string `pg:"msg"`
Extra map[string]interface{} `pg:"extra"`
}
type LogType string
const (
Debug LogType = "debug"
Info LogType = "info"
Warning LogType = "warning"
Error LogType = "error"
)
func main() {
db := pg.Connect(&pg.Options{Addr: "localhost:5432"})
err := db.Ping(context.Background())
panicOnError(err)
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS logs (
id serial PRIMARY KEY,
type text NOT NULL,
msg text NOT NULL,
extra jsonb DEFAULT '{}'
);
`)
panicOnError(err)
log := Log{
Msg: "hello world",
Type: Info,
Extra: map[string]interface{}{
"1": []string{"qwerty"},
},
}
_, err = db.Model(&log).Insert()
panicOnError(err)
}
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
I am not sure how to resolve this issue. If there's an opportunity to use custom logger, we will able to just filter this warning. But github.com/go-pg/pg/internal.Warn
is a global variable
This should be fixed by #1754 . If this continue to be a problem we may try something like pg:"name:type"
for cases like this.
I'd like to re-open this issue.
ServerType string `json:"type" pg:"type"`
this type of usage is still invalid, throws ServerModel.ServerType tag name "type" is also an option name; is it a mistake?