v8.0.7 / v10.8.0 #23502 error with `pg:",notnull"` annotation.
rive-n opened this issue · 1 comments
rive-n commented
type ms_backend_customuser struct {
tableName struct{} `sql:"ms_backend_customuser"`
Id int64
password string `pg:",notnull"`
lastLog time.Time
createdAt time.Time
modifiedAt time.Time
Email string `pg:",notnull"`
firstName string `pg:",notnull"`
lastName string `pg:",notnull"`
suRights bool
admin bool
active bool
}
This is struct that implemets my database table. So, I just want to Insert some values here, like:
func handleRegister(response http.ResponseWriter, request *http.Request, db *pg.DB) {
switch request.Method {
case "POST":
user := &ms_backend_customuser{
Email: request.FormValue("email"),
password: request.FormValue("password"),
}
var mail string
if err := db.Model(user).Column("email").Where("email = ?", user.Email).Select(&mail); err == pg.ErrNoRows {
// TODO: flash message
if err := user.createHash(); err != nil {
log.Println(err.Error())
http.Error(response, err.Error(), http.StatusInternalServerError)
}
log.Println(user.Email, user.password)
if _, err := db.Model(user).Insert(); err != nil {
log.Println(err.Error())
} else {
log.Println("User successfully registered!\n", user.Email, user.password)
}
} else {
log.Println(err.Error())
}
case "GET":
default:
_, _ = response.Write([]byte("Method not allowed"))
}
renderTemplate(response, request, "register.html", "")
}
So, if you can see - not null and other annotations are not working.
This is output of all my methods:
riven1@riven.com pbkdf2_sha256$216000$/mbHQRbzkBfc04JE$9jY/jfLHsi/4XbCK4LJtmsky0GuS/aw03QXxvNkXvmE=
So nothing is wrong on my side i guess. I tryed to find something about #23502, but the only thing is about changing annotations. Probably, that did not help.
Full error:
ОШИБКА #23502 значение NULL в столбце "password" отношения "ms_backend_customuser" нарушает ограничение NOT NULL
rive-n commented
Well, problem was in all other annotations, PK and private sections of struct:
- - [ PK ]
- - [ Capturing all requests in RAW format ]
- - [ Resolving problem with private struct sections in go ]
- private in golang. Pretty nice description.
- PK. If in your DB PK is stored like this:
id integer NOT NULL DEFAULT nextval('ms_backend_customuser_id_seq'::regclass)
, you should remove ID from your struct. - The DELETED part of go-pg DOC: how to create row sql parser from ORM queries:
This solution "forked" from here: #1366
- Create empty structure like this:
type dbLogger struct{}
- Create this 2 methods from sources like this:
func (d dbLogger) BeforeQuery(c context.Context, q *pg.QueryEvent) (context.Context, error) {
return c, nil
}
func (d dbLogger) AfterQuery(c context.Context, q *pg.QueryEvent) error {
fq, _ := q.FormattedQuery()
fmt.Println(string(fq))
return nil
}
Horray! Now you should see every request to your Database!