Skip to content

NullEnums.Value() breaks SQL-Execution #1880

Closed
@Neokil

Description

@Neokil

Version

1.15.0

What happened?

I have an Entity that I want to either select all or filter by mode. The SQL looks like this:

SELECT *
FROM entity
WHERE (mode IN (sqlc.narg(mode)) OR sqlc.narg(mode) IS NULL)

This generates a struct called NullMode and a Value() function for it

type Mode string

type NullMode struct {
	Mode  Mode
	Valid bool // Valid is true if String is not NULL
}

func (ns NullMode) Value() (driver.Value, error) {
	if !ns.Valid {
		return nil, nil
	}
	return ns.Mode, nil
}

Now when this gets executed I am getting the following Error-Message:

sql: converting argument $1 type: non-Value type analytics.Mode returned from Value

This can be easily fixed by adding a string-cast to the Value-Return like this:

return string(ns.Mode), nil

Relevant log output

No response

Database schema

CREATE TYPE mode AS ENUM (
    'live',
    'shadow'
);

CREATE TABLE entity (
    id   SERIAL PRIMARY KEY,
    mode mode NOT NULL DEFAULT 'shadow'
)

SQL queries

No response

Configuration

No response

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions