Skip to content

Global Go type overrides ignore the engine selector #4624

Description

@xue-ding-e

Version

v1.31.1, built from main at bdbe55db3ee08745827090d4aad1781bea2ee4ee.

What happened?

Global Go overrides specify an engine, but overrides for other engines are still applied. With PostgreSQL, MySQL and SQLite sections in one config, all three generated packages use the first matching Go type override and the last matching struct tag, regardless of the engine.

For the reproduction below:

Output Expected Value / Code types Actual Value / Code types
PostgreSQL string / json.RawMessage string / json.RawMessage
MySQL sql.NullString / []byte string / json.RawMessage
SQLite []byte / string string / json.RawMessage

All three also receive backend:"sqlite" instead of their own engine's tag. This affects model fields and query parameters. The documented engine selector should restrict each rule to its matching engine.

Relevant log output

sqlc generate exits successfully and produces the incorrect types above.

Database schema

schema.sql:

CREATE TABLE entries (
    id integer NOT NULL,
    value text NOT NULL,
    code text NOT NULL
);

SQL queries

query.sql:

-- name: FindEntry :one
SELECT id, value, code FROM entries
WHERE value = sqlc.arg(value) AND code = sqlc.arg(code);

Configuration

sqlc.yaml:

version: "2"
overrides:
  go:
    overrides:
      - db_type: text
        engine: postgresql
        go_type: string
        go_struct_tag: 'backend:"postgresql"'
      - db_type: text
        engine: mysql
        go_type: "database/sql.NullString"
        go_struct_tag: 'backend:"mysql"'
      - db_type: text
        engine: sqlite
        go_type:
          type: byte
          slice: true
        go_struct_tag: 'backend:"sqlite"'
      - column: entries.code
        engine: postgresql
        go_type:
          import: encoding/json
          type: RawMessage
      - column: entries.code
        engine: mysql
        go_type:
          type: byte
          slice: true
      - column: entries.code
        engine: sqlite
        go_type: string
sql:
  - engine: postgresql
    schema: schema.sql
    queries: query.sql
    gen:
      go:
        package: pg
        out: pg
  - engine: mysql
    schema: schema.sql
    queries: query.sql
    gen:
      go:
        package: mysql
        out: mysql
  - engine: sqlite
    schema: schema.sql
    queries: query.sql
    gen:
      go:
        package: sqlite
        out: sqlite

What operating system are you using?

Windows, Go 1.26.5.

What database engines are you using?

PostgreSQL, MySQL and SQLite; this reproduction uses static SQL analysis and needs no database server.

What type of code are you generating?

Go.

The same behavior is reproducible with a version 1 config. Filtering global overrides by the request engine before parsing them fixes the reproduction. AI assistance was used for the reproduction and proposed fix.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions