sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Generated `Params` don't allow nullable values when `COALESCE` is used

Open bbkane opened this issue 1 year ago • 1 comments

Version

1.25.0

What happened?

I'm trying this

I expected the generated UpdateEnvParams to contain nullable types since:

  • the schema contains NOT NULL fields
  • the update SQL contains COALESCE(?, <fieldname>)

Since the field is constrained as NOT NULL in the schema, then this allows updating a field when ? is not null, and leaving the field alone if ? is null.

Ideally, the user would pass some null fields and some not-null fields and I could pass those through to sqlc transparently via the generated UpdateEnvParams that would contain nullable types to pass to the SQL.

This code is hosted at https://github.com/bbkane/namedenv/

Relevant log output

No logs

Database schema

CREATE TABLE env (
    "id" INTEGER PRIMARY KEY,
    "name" TEXT NOT NULL,
    "comment" TEXT,
    "create_time" TEXT NOT NULL,
    "update_time" TEXT NOT NULL,
    UNIQUE(name)
) STRICT;

SQL queries

-- name: UpdateEnv :exec
UPDATE env SET
    name = COALESCE(?, name),
    comment = COALESCE(?, comment),
    create_time = COALESCE(?, create_time),
    update_time = COALESCE(?, update_time)
WHERE id = ?;

Configuration

version: "2"
sql:
  - engine: "sqlite"
    queries: "envsqlc.sql"
    schema: "sqlite/connect/embedded_migrations"
    gen:
      go:
        package: "sqlcgen"
        out: "sqlite/sqlcgen"

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

SQLite

What type of code are you generating?

Go

bbkane avatar Jan 10 '24 05:01 bbkane

I'll be able to avoid this issue once https://github.com/sqlc-dev/sqlc/issues/2800 is implemented (I think can force a parameter to be null - still not sure how to override names in my SQLite queries).

Also see : https://github.com/sqlc-dev/sqlc/issues/2183

Duplicate of https://github.com/sqlc-dev/sqlc/issues/937

bbkane avatar Jan 10 '24 05:01 bbkane