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

pointer to struct when using jsonb #3189

Unanswered
nicolasparada asked this question in Q&A
Discussion options

Let's say I have this schema:

CREATE TABLE IF NOT EXISTS pipelines (
 id VARCHAR NOT NULL PRIMARY KEY DEFAULT uuid_to_ulid(gen_random_ulid()),
 config JSONB NOT NULL
);

I configured sqlc to use a custom struct for the pipelines.config JSONB.

type PipelineConfig struct {
 SomeField string
}

I was able to do it using this config:

version: "2"
sql:
 - engine: postgresql
 schema: db/migrations/*.sql
 queries: db/queries/*.sql
 gen:
 go:
 out: db
 sql_package: pgx/v5
 emit_db_tags: true
 emit_json_tags: true
 emit_pointers_for_null_types: true
 emit_sql_as_comment: true
 json_tags_id_uppercase: true
 json_tags_case_style: camel
 overrides:
 - column: pipelines.config
 go_type:
 type: PipelineConfig

All great, the generated model uses my struct.

My problem comes down when I write an UPDATE query with an optional argument like so:

-- name: UpdatePipeline :exec
UPDATE pipelines SET
 config = COALESCE(sqlc.narg(config), config)
WHERE id = @pipeline_id;

I was hoping the generated update params to use a pointer, but it doesn't.

I can go to the sqlc config and do this:

- column: pipelines.config
 nullable: true
 go_type:
 type: PipelineConfig
 pointer: true

And that generates a pointer on the update params, but it also generates a pointer for the model.
I want the model to not use a pointer, only on the update params. Is there a way to do it?


Note: I know I could write the query differently to overcome this. Maybe something like:

UPDATE pipelines SET
 config = (
 CASE WHEN @set_config::bool
 THEN @config::jsonb
 ELSE config
 END
 )
WHERE id = @pipeline_id;

But since I'm able to use proper pointers for other primitive types when using sqlc.narg() I would like to stick to using pointers.

You must be logged in to vote

Replies: 0 comments

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

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