sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Add a NULL value to a varchar array

Open nakem1 opened this issue 3 years ago • 4 comments

Version

1.13.0

What happened?

I have a query

-- name: Test :exec
INSERT INTO test (id, body)
VALUES (
  UNNEST($1::uuid[]), UNNEST($2::varchar[]));

there is a generated struct

type TestParams struct {
	Column1     []uuid.UUID   `json:"column_1"`
	Column2     []string          `json:"column_2"`
}

sometimes i need to pass to body a null value. I can't pass NULL to string. As a quick fix, I changed the generated file. Yes, I know it's a crime against humanity, but it solves my problem:))

I changed []string -> []interface{} in the TestParams struct.

Relevant log output

No response

Database schema

create table test
(
    id            uuid                                                 not null,
    body          varchar,
)

SQL queries

No response

Configuration

No response

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

nakem1 avatar May 23 '22 23:05 nakem1

Using sqlc.narg() doesn't work: https://play.sqlc.dev/p/1086edbe69a9fcee4861557b9edfcb9c1294294596d41d5095d9734bd311d772

kyleconroy avatar Sep 26 '23 08:09 kyleconroy

Is there any solution so far?

Abdullah-AlAttar avatar Jul 08 '24 19:07 Abdullah-AlAttar

any other workaround for this?

bagashiz avatar Mar 07 '25 07:03 bagashiz

any other workaround for this?

@bagashiz, @Abdullah-AlAttar, a bit hacky but works (if you don't require empty strings ) sql INSERT INTO test (id, body) SELECT UNNEST($1::uuid[]), NULLIF(UNNEST($2::varchar[]), '');

stepan-romankov avatar May 02 '25 20:05 stepan-romankov