sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.44k stars 803 forks source link

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

Open bbkane opened 10 months ago

bbkane commented 10 months ago

Version

1.25.0

What happened?

I'm trying this

I expected the generated UpdateEnvParams to contain nullable types since:

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 commented 10 months ago

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