scany
scany copied to clipboard
Decoding into an Array of Structs with an Embedded Field Fails
This snippet fails:
type Person struct {
NameFirst string `db:"name_first"`
NameLast string `db:"name_last"`
}
type EmbeddedPerson struct {
Person `db:""`
}
var people [][]EmbeddedPerson
err = pgxscan.Select(
context.Background(),
conn, // Left unprovided but unimportant.
&people,
`SELECT
ARRAY_AGG(person)
FROM UNNEST(
ARRAY['Alan', 'Turing'],
ARRAY['Marie', 'Curie']
) AS person (name_first, name_last);`,
)
if err != nil {
panic(err)
}
fmt.Println(people)
The error is:
scanning all: scanning: scanning: doing scan: scanFn: scany: scan row value into a primitive type: can't scan into dest[0]: failed to scan array element 0: cannot scan text (OID 25) in binary format into *package.Person
However [][]Person
works fine as does []EmbeddedPerson
, provided you change ARRAY_AGG(person)
into person.*
. So the problem seems to only be when trying to decode an array into an embedded struct.