I'm using pgx v5 and scany v2. Apologies in advance since I'm just a casual Go user and may have missed something obvious.
I've got the following code to insert (with a RETURNING *):
rows, err := s.db.Query(context.Background(), stmt, ...args)
if err != nil {
return nil, err
}
if err := pgxscan.ScanOne(entity, rows); err != nil {
/*
// https://github.com/jackc/pgx/wiki/Error-Handling - how can I make use of this?
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
fmt.Println(pgErr.Message) // => syntax error at end of input
fmt.Println(pgErr.Code) // => 42601
}
*/
return nil, err
}
There's a unique constraint, so if I try to insert the same record twice I'll receive the following:
scany: rows final error: ERROR: duplicate key value violates unique constraint ...
How can I get the error code + other details? Thanks
I'm using pgx v5 and scany v2. Apologies in advance since I'm just a casual Go user and may have missed something obvious.
I've got the following code to insert (with a
RETURNING *
):There's a unique constraint, so if I try to insert the same record twice I'll receive the following:
How can I get the error code + other details? Thanks