-
Notifications
You must be signed in to change notification settings - Fork 923
pointer to struct when using jsonb #3189
-
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.
Beta Was this translation helpful? Give feedback.