Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

OVerride type of aggregated JSON with json_agg #3275

Unanswered
svenwltr asked this question in Q&A
Discussion options

Hello!

I have this query:

-- name: ListSBOMComponents :many
select
 component_name,
 json_agg(version) as versions,
 json_agg(license_ids) as licenses
from sbom_components
group by component_name;

Where component_name, version and license_ids are all strings.

The resulting type is this:

type ListSBOMComponentsRow struct {
	ComponentName string `json:"componentName"`
	Versions []byte `json:"versions"`
	Licenses []byte `json:"licenses"`
}

I am using pgx where it is possible to specify which types JSON objects deserialize to. (so this), but I don't know what to put into the column field.

What I tried is:

 - column: "sbom_components.versions"
 go_type:
 type: "[]string"

What I expect is

type ListSBOMComponentsRow struct {
	ComponentName string `json:"componentName"`
	Versions []string `json:"versions"`
	Licenses []string `json:"licenses"`
}

But that did not work as expected. Any ideas on this?

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

It so because of sbom_components.versions is not a column. It's an aliased tuple value. In you're case you can use type override for db_type not a column. You can create that type with CREATE TYPE or just CREATE DOMAIN for existing json(b) type. I'am not sure sqlc will (un)marshall json when dealing with db_type, not column, but most likely it will.

CREATE DOMAIN versions AS jsonb;
 - db_type: "versions"
 go_type:
 type: "[]string"
You must be logged in to vote
2 replies
Comment options

Or you can try:

 - db_type: "versions"
 go_type:
 type: "string"
 slice: true
Comment options

And of course you need to explicitly cast type versions in your query

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /