You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've searched through your source code and documentation but I cannot find a solution.
I want to select an entry as well as for all entries in another table that are related to it.
A simplified example would like this:
-- schema.sqlCREATETABLEauthors (
id BIGSERIALPRIMARY KEY,
name TEXTNOT NULL
);
CREATETABLEbooks (
id BIGSERIALPRIMARY KEY,
author_id BIGINTNOT NULLREFERENCES authors(id),
title TEXTNOT NULL
);
-- query.sql-- name: GetAuthorsWithBooks :manySELECTa.id,
a.name,
jsonb_agg(jsonb_build_object('id',
b.id,
'author_id',
b.author_id,
'title',
b.title,)
) FILTER (WHEREb.idIS NOT NULL)
AS books
FROM authors a
LEFT JOIN books b
ONa.id=b.author_idGROUP BYa.id;
I would like to produce a result model similiar to this example:
typeGetAuthorsWithBooksstruct {
IDint64`db:"id" json:"id"`Namestring`db:"name" json:"name"`// I would like to get this generated attributeBooks []Book`db:"books" json:"books"`// But instead i get thisBooks []byte`db:"books" json:"books"`
}
typeBookstruct {
IDint64`json:"id"`AuthorIDint64`json:"author_id"`Titlestring`json:"title"`
}
I tried to use overrides to achieve the desired result model and these overrides work when configured by type but not by column name:
version: "2"sql:
- schema: "internal/db/schema.sql"queries: "internal/db/query.sql"engine: "postgresql"gen:
go:
package: "dbgen"out: "internal/db/gen"emit_db_tags: trueemit_json_tags: truejson_tags_id_uppercase: falsejson_tags_case_style: snakeoverrides:
- db_type: json # This one works but overrides all json columns to books which interferes with other queries.go_type:
type: "Book"slice: true
- column: "GetAuthorsWithBooks.books"# This config is not documented but I did not know what else to use as the correct table name.go_type:
type: "Book"slice: true
It does not work and does not produce any errors or something else (version 1.29). It just gets ignored as far as I can tell.
Does anyone of a way to archive this without using (materialized) views? Just with plain sqlc?
Or if not does anyone know a way to override a type for a column that is not an existing column in an existing table?
I mean when I would run SELECT now()::timestamptz AS current_time what would I have to fill in here:
sql:
- gen:
go:
overrides:
- column: "what use here?"# I know you would use override by type here but how would I override this column by name?go_type:
import: timetype: Time
The text was updated successfully, but these errors were encountered:
Uh oh!
There was an error while loading. Please reload this page.
I've searched through your source code and documentation but I cannot find a solution.
I want to select an entry as well as for all entries in another table that are related to it.
A simplified example would like this:
I would like to produce a result model similiar to this example:
I tried to use overrides to achieve the desired result model and these overrides work when configured by type but not by column name:
It does not work and does not produce any errors or something else (version 1.29). It just gets ignored as far as I can tell.
Does anyone of a way to archive this without using (materialized) views? Just with plain sqlc?
Or if not does anyone know a way to override a type for a column that is not an existing column in an existing table?
I mean when I would run
SELECT now()::timestamptz AS current_time
what would I have to fill in here:The text was updated successfully, but these errors were encountered: