sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Wrong param type when using COALESCE

Open said-saifi opened this issue 4 years ago • 8 comments

Summary

COALESCE works perfectly when your column is already nullable (no NOT NULL), however if your column does not accept null values then if you use COALESCE your argument in GO will be just be a string for example instead of sql.NullString

Version

v1.6.0

How to reproduce

-- Example queries for sqlc
CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  name text NOT NULL,
  bio  text
);

-- name: UpdateAuthor :exec
UPDATE authors
SET name=COALESCE($2, name) WHERE id=$1;

Or click here

Expected Results

type UpdateAuthorParams struct {
	ID   int64
	Name sql.NullString
}

Actual Results

type UpdateAuthorParams struct {
	ID   int64
	Name string
}

So as you can see Name field in UpdateAuthorParams is just of type string, so using COALESCE with that will be useless.

Possible Alternatives

If you don't have to make your column NOT NULL in the database then this works as shown here

said-saifi avatar Mar 11 '21 07:03 said-saifi