georgysavva / scany

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

Functions are not supported in queries #15

Closed dclipca closed 3 years ago

dclipca commented 3 years ago

I am performing the following query using pgx:

err := pgxscan.Select(context.Background(), db.pool, &things, `SELECT ST_AsBinary("coordinate") FROM "thing"`)

After executing it, I am getting the following error:

column: 'st_asbinary': no corresponding field found, or it's unexported in main.SafePlayer

It seems that functions are not supported in queries. Is it possible to add this functionality?

georgysavva commented 3 years ago

Hi. For scany it doesn’t matter is it a function or a regular field. It works with column names and the column name is st_asbinary in that case. The error that you posted means that result rows contain column st_asbinary, but your strict doesn't. I believe you wanted it to be coordinate instead. You can fix it by adding an alias in the SQL:

SELECT ST_AsBinary("coordinate") AS coordinate FROM "thing"

Otherwise, you can add struct tag db:"st_asbinary" to the one of main.SafePlayer fields, but the solution above makes more sense for me.

dclipca commented 3 years ago

@georgysavva This works perfectly. Thanks for your help!