Closed graineri closed 3 years ago
Hello! I understand the issue, but it's not related to scany capabilities. Scany isn't an ORM, when you define your type like that:
type User struct {
Name string
Customers []Customer
}
The only thing scany can do with the Customers
field is to pass it as a reference to the underlying library: &Customers
. After that pgx is responsible for handling []Customer
type. pgx indeed tries to fill it as JSON data and you are right to build a JSON object inside the inner SELECT
statement.
Since scany has nothing to do with JSON part, you need to follow Go JSON serialization rules, instead of type.id
and type.name
(scany style) try to do:
json_agg(
json_build_object(
'name', customer.name,
'type', json_build_object(
'id', ct.id,
'name', ct.name,
)
)
)
I hope it helps!
Yes, that's correct. I ended up doing it in a similar way. Thanks Georgy.
Issue: customer type does not get populated. Only customer name does.