Skip to content

sqlc.Narg does not work with enums #1683

Closed

Description

@Emyrk

Version

1.14.0

What happened?

sqlc.narg is a great addition. Unfortunately, it does not seem to work for enum types. I would really like to be able to use nullable enums, as empty strings are invalid enums and throw an error.

In the example provided, it would be great if the method definition used *Colors to make it nullable:

func (q *Queries) GetBoxes(ctx context.Context, color *Colors) ([]Box, error) {

The actual method does not accept a nullable argument.

func (q *Queries) GetBoxes(ctx context.Context, color Colors) ([]Box, error) {

Relevant log output

No response

Database schema

CREATE TYPE colors AS ENUM (
    'red',
    'blue',
    'green'
);

-- Example queries for sqlc
CREATE TABLE boxs (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  color colors
);

SQL queries

-- name: GetBoxes :many
SELECT * FROM boxs
WHERE 
CASE
	WHEN sqlc.narg('color') :: colors != null THEN
		color = sqlc.narg('color')
	ELSE true
END;

Configuration

No response

Playground URL

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

What operating system are you using?

Linux

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Jun 16, 2022
  2. eullerpereira94 commented on Jun 24, 2022

    @eullerpereira94
    Contributor

    The thing about this behavior you encountered is that there is no way to handle null values for the Colors enum. There is sql.NullString, sql.NullTime, sql.NullBool, but there isn't a NullColors type that can handle null values. Later, I will link a repo showing how to use enums that can be nullable.

  3. Emyrk commented on Jun 24, 2022

    @Emyrk
    ContributorAuthor

    Would like to see that example!

  4. eullerpereira94 commented on Jun 24, 2022

    @eullerpereira94
    Contributor

    This repository shows to how to do it: https://github.com/eullerpereira94/sqlc_enum_test

    Notice that you have to manually create a type to handle your enums and a second type that implements both sql.Scanner and driver.Valuer to handle your enums when they can be nullable.

    Another thing that happens is that sqlc generates a type for your enum regardless of any type that you might provide to it. Maybe I can work a PR so that sqlc might generate the nullable type on its own.

  5. Emyrk commented on Jun 24, 2022

    @Emyrk
    ContributorAuthor

    @eullerpereira94 Thanks for the example. I will take a look and replicate in my repo if it's better than my current method.

    It would be great to autogen it as all enums should be primitive types under the hood!

  6. danielbprice commented on Jun 28, 2022

    @danielbprice
    Contributor

    There are two open MRs for this, see #1582 and #1485

  7. eullerpereira94 commented on Jun 28, 2022

    @eullerpereira94
    Contributor

    @danielbprice well, I did'nt know. I saw the problem and tried to fix it. The other two proposals seem to have more work done than mine. And seeing that they have proper functioning code already, I will just wait for when a new version with this feature is released.

  8. sbres commented on Jul 5, 2022

    @sbres
    Contributor

    I'm waiting to get the PR merged as well, I'm starting to get worried it won't get merged anytime soon

  9. sbres commented on Jul 5, 2022

    @sbres
    Contributor

    Never mind it just got merged 🎉

  10. kyleconroy commented on Aug 29, 2022

    @kyleconroy
    Collaborator
  11. removed
    triageNew issues that hasn't been reviewed
    on Aug 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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