Skip to content

Unable to override Postgres array column to slice of enum in Go #4359

@cellulosa

Description

@cellulosa

Version

1.30.0

What happened?

I’m trying to map a Postgres array of integers (smallint[]) to a slice of a protobuf enum in Go using sqlc overrides, but I can’t get it to work.

sqlc fails to parse the type because it does not allow [] in the type field. It seems there is no way to override a Postgres array column to a slice of a custom Go type (like an enum). It would be great if sqlc could support mapping Postgres array types to Go slices of custom types (enums, structs, etc.) directly.

I expected sqlc would generate (at the back of a type: "[]WorkArrangement"):

type GetHuntsRow struct {
    WorkArrangements []commonv1.WorkArrangement    `json:"work_arrangements"`
}

Relevant common.proto enum definition:

enum WorkArrangement {
  REMOTE = 1;
  HYBRID = 2;
  ON_LOCATION = 3;
}

Relevant log output

error generating code: expected 'IDENT', found '['

Database schema

CREATE TABLE hunts (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    -- common.proto WorkArrangement: 1=remote; 2=hybrid; 3=onlocation
  work_arrangements smallint[] NOT NULL DEFAULT '{}'
);

SQL queries

Configuration

version: "2"

plugins:
  - name: "golang"
    wasm:
      url: "https://downloads.sqlc.dev/plugin/sqlc-gen-go_1.5.0.wasm"
      sha256: "4ca52949f4dc04b55188439f5de0ae20af2a71e3534b87907f2a7f466bda59ec"

sql:
  - schema: "migrations"
    engine: "postgresql"
    codegen:
      - plugin: "golang"
        out: "generated"
        options:
          package: "sqlc"
          sql_package: "pgx/v5"
          emit_json_tags: true
          emit_pointers_for_null_types: true
          overrides:
            - column: "hunts.work_arrangements"
              go_type:
                import: "github.com/user/company/go-packages/connectrpc/generated/common/v1"
                package: "commonv1"
                type: "WorkArrangement"

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

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions