Skip to content

Wrong param type when using COALESCE #937

Open
@said-saifi

Description

@said-saifi

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions