Skip to content
Discussion options

You must be logged in to vote

Use json.RawMessage instead of []byte for JSONB fields. json.RawMessage is []byte under the hood but implements json.Marshaler to output raw JSON rather than Base64.

Override the type mapping in sqlc.yaml:

version: "2"
sql:
  - engine: "postgresql"
    queries: ["query.sql"]
    schema: ["schema.sql"]
    gen:
      go:
        package: "model"
        out: "model"
        sql_package: "pgx/v5"
        overrides:
          - db_type: "jsonb"
            go_type: "encoding/json.RawMessage"

After regenerating, the field becomes:

type Category struct {
    SpecTemplate json.RawMessage `json:"spec_template"`
}

json.RawMessage marshals directly as the raw JSON value (object, array, etc.) witho…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by sanbei101
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants