georgysavva / scany

Library for scanning data from a database into Go structs and more
MIT License
1.29k stars 68 forks source link

Issue with bytea conversions #93

Closed sheldondz closed 2 years ago

sheldondz commented 2 years ago
type Token struct {
    UserID.  string `json:"user_id" db:"user_id"`
    TokenID string `json:"token_id" db:"token_id"`
}

sqlQuery := "SELECT encode(token_id,'hex') from users"

var tokens []*Token
err := pgxscan.Select(ctx, db, &tokens, sqlQuery) 

Get the following error where token_id is bytea type

scanning all: scanning: scanning: doing scan: scanFn: scany: column: 'encode': no corresponding field found, or it's unexported
georgysavva commented 2 years ago

Hi. The problem here is that encode(token_id, 'hex') changes the column name from token_id to encode. So the solution is to explicitly set an alias in the SELECT statement:

sqlQuery := `SELECT encode(token_id,'hex') AS "token_id" from users`

Feel free to reopen the issue if you still have problems.