Replies: 1 comment
-
We're still deciding on how best to embed nested structs in return values. Please see the discussion here #363 To answer your specific question: sqlc will also return a struct that mirrors the results from the database. Let's assume that your tables look like this: CREATE TABLE users (
id BIGSERIAL NOT NULL,
name TEXT
);
CREATE TABLE comments (
user_id BIGINT NOT NULL,
body TEXT
); And this is the query you select *
from users
left join comments on comments.user_id = users.id With a bit of sample data, this returns the following data.
Which translates into the final Go struct. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
When you have a query like the following:
How do people deal with this? It seems like sqlc's result is:
I think intuitively in Go you'd rather a result like so:
It's not obvious from the docs how to handle this common use case. Is it expected that users collect all the user ids returned from a first query that only fetches users, then subsequently perform another query
where user_id = any($1::[]integer)
instead of joining?Just trying to solve this for the general case because I'm finding it a bit clunky. It's not trivial (boilerplate-wise) to convert the sqlc result into the desired result (need reflect helpers or large conversion functions), but it requires many more queries to the database.
I feel like I'm missing something. Can people share how they're using sqlc to retrieve and use data like the above?
Beta Was this translation helpful? Give feedback.
All reactions