sqlc-dev / sqlc

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

Field name not respected when using `NULLIF` in insert query #1506

Open go-mez opened 2 years ago

go-mez commented 2 years ago

Version

1.12.0

What happened?

When generating an insert query using the NULLIF function, the field name is not respected and will be generated as nullif.

Relevant log output

type CreateAlarmParams struct {
    NULLIF    interface{}
    CreatedBy sql.NullInt32
}

Database schema

CREATE TABLE alarms (
    alarm_title varchar(30),
    created_by int(11)
);

SQL queries

-- name: CreateAlarm :execresult
INSERT INTO alarms (
  alarm_title, 
  created_by
) VALUES 
(
  NULLIF(?, ''), 
  ?
);

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "mysql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/fbe1bfb0158f637dd53b3ad90e19ecdc59c27fbf7568cd332b9a120af81f39cc

What operating system are you using?

macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

skabbes commented 2 years ago

@go-mez would my proposal for giving the developer control of the input and output parameters to the query (including naming, nullability, etc) solve this issue for you?

https://github.com/kyleconroy/sqlc/issues/1506

kyleconroy commented 1 year ago

sqlc.arg can at least get you a better name https://play.sqlc.dev/p/1642ad7238085024e55559bdb875cf8928b39ab0521045df66289a6bc798505d