Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-nullable parameters generated when using overridden type + sqlc.narg + COALESCE #3494

Open
bbkane opened this issue Jul 20, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@bbkane
Copy link

bbkane commented Jul 20, 2024

Version

1.26.0

What happened?

I've got a SQLite table with a TEXT data type called create_time . I update create_time with the following update query:

-- See https://docs.sqlc.dev/en/latest/howto/named_parameters.html#nullable-parameters
-- name: EnvUpdate :exec
UPDATE env SET
    create_time = COALESCE(sqlc.narg('create_time'), create_time)
WHERE id = sqlc.arg('id');

This works fine and generates an update query parameter with a string pointer as I expect when using sqlc.narg:

type EnvUpdateParams struct {
	CreateTime *string
	ID         int64
}

However, if I create the type type SQLiteTime string and set create_time to use this type in sqlc.yaml (which I would like to do so I can attach methods to the type), the generated update code does NOT use a pointer as I expect:

type EnvUpdateParams struct {
   CreateTime sqlite.SQLiteTime
   ID         int64
}

I would expect it to look like:

type EnvUpdateParams struct {
	CreateTime *sqlite.SQLiteTime
	ID         int64
}

Not having it be a pointer means I can't assign nil to it, which breaks my COALESCE SQL update query.

As a full reproducible demo of this bug, I've made https://github.com/bbkane/sqlc-override-nullable . It has a using-text package that demos the correct codegen, and a using-overrides that demos the incorrect codegen.

Some more context about WHY I'd like to be able to to this:

I'm writing an environment variable manager CLI that needs to store times. Instead of the "sqlc-native" format as a stringified time.Time, I want to store times in the "sqlite-native" format as a text column so I can run datetime queries on the columns.

Finally, thank you so much for writing sqlc! I've started GitHub sponsoring you because this tool removes a lot of the drudgery of working with SQL from Go, and I really appreciate that.

Relevant log output

No response

Database schema

CREATE TABLE env (
    id INTEGER PRIMARY KEY,
    create_time TEXT NOT NULL
) STRICT;

SQL queries

-- See https://docs.sqlc.dev/en/latest/howto/named_parameters.html#nullable-parameters
-- name: EnvUpdate :exec
UPDATE env SET
    create_time = COALESCE(sqlc.narg('create_time'), create_time)
WHERE id = sqlc.arg('id');

Configuration

sql:
  - engine: sqlite
    gen:
      go:
        emit_pointers_for_null_types: true
        out: sqlcgen
        package: sqlcgen
        overrides:
          - column: "env.create_time"
            go_type: "go.bbkane.com/sqlc-override-nullable/using-override/sqlite.SQLiteTime"
    queries: queries
    schema: migrations
# yq wants to double quote "2", but yamllint wants it single-quoted
# yamllint disable-line rule:quoted-strings
version: "2"

Playground URL

No response

What operating system are you using?

Linux, macOS

What database engines are you using?

SQLite

What type of code are you generating?

Go

@bbkane bbkane added bug Something isn't working triage New issues that hasn't been reviewed labels Jul 20, 2024
@bbkane
Copy link
Author

bbkane commented Jul 21, 2024

Also see #2800

@kyleconroy kyleconroy removed the triage New issues that hasn't been reviewed label Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants