Skip to content

Nullable enum columns are not recognized as nullable by generated code #1660

Closed
@jackdelab

Description

@jackdelab

Version

1.10.0

What happened?

Generated code causes an error when it tried to scan a row from a table with a nullable enum column.

Relevant generated code:

type Resource struct {
	ID             int32            `json:"id"`
	KeyState       ResourceKeyState `json:"key_state"`
}

type ResourceKeyState string

const (
	ResourceKeyStateNOTYETACTIVE      ResourceKeyState = "NOT_YET_ACTIVE"
	ResourceKeyStateACTIVE            ResourceKeyState = "ACTIVE"
	ResourceKeyStateINACTIVE          ResourceKeyState = "INACTIVE"
	ResourceKeyStateDELETIONSCHEDULED ResourceKeyState = "DELETION_SCHEDULED"
)

func (e *ResourceKeyState) Scan(src interface{}) error {
	switch s := src.(type) {
	case []byte:
		*e = ResourceKeyState(s)
	case string:
		*e = ResourceKeyState(s)
	default:
		return fmt.Errorf("unsupported scan type for ResourceKeyState: %T", src)
	}
	return nil
}

Expected:
Generated code should recognize that the column can have null values, and scan values from it accordingly.

Relevant log output

{"log.level":"ERROR","@timestamp":"2022-06-01T03:58:17.096Z","log.origin":{"file.name":"<REDACTED>.go","file.line":179},"message":"sql: Scan error on column index 7, name \"key_state\": unsupported scan type for ResourceKeyState: <nil> (rest of log message REDACTED)"}

Database schema

CREATE TABLE `resource` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `key_state` enum('NOT_YET_ACTIVE','ACTIVE','INACTIVE','DELETION_SCHEDULED') DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

SQL queries

/* name: GetResourceById :one */
SELECT * FROM resource
WHERE id = ? ;

Configuration

{
  "version": "2",
  "sql": [
    {
      "schema": "sqlc/<REDACTED>.sql",
      "queries": "sqlc/<REDACTED>.sqlc.sql",
      "engine": "mysql",
      "gen": {
        "go": {
          "emit_json_tags": true,
          "package": "sqlcgen",
          "out": "components/sqlc"
        }
      }
    }
  ]
}

Playground URL

No response

What operating system are you using?

Linux, macOS

What database engines are you using?

MySQL

What type of code are you generating?

Go

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