Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JSONB fields are scanned as[]byte and serialized to Base64 in JSON, not as objects#4150

Unanswered
sanbei101 asked this question inQ&A
Discussion options

Problem Description

When usingsqlc with PostgreSQL andpgx/v5, columns of typeJSONB are generated as[]byte in the Go model. This causes standard JSON marshaling (e.g., via Gin orencoding/json) to encode the field as aBase64 string instead of the expectedraw JSON object.

For example, aspec_template JSONB NOT NULL column becomes:

typeCategorystruct {SpecTemplate []byte`json:"spec_template"`}

When serialized:

{  "spec_template": "eyJuYW1lIjogInRlc3QifQ=="  // ← Base64, not {"name":"test"}}

how should i do?
here is all code:

version: "2"sql:  - engine: "postgresql"    queries:      - "query.sql"    schema:      - "schema.sql"    gen:      go:        package: "model"        out: "model"        sql_package: "pgx/v5"
CREATE TABLE category (    id             BIGSERIAL PRIMARY KEY,    name           VARCHAR(100) NOT NULL,    parent_id      BIGINT REFERENCES category(id) DEFAULT NULL,    spec_template  JSONB NOT NULL,    sort           INT NOT NULL DEFAULT 0,    created_at     TIMESTAMP NOT NULL DEFAULT NOW());
const listCategories = `-- name: ListCategories :manySELECT id, name, parent_id, spec_template, sort, created_at FROM category ORDER BY sort ASC, created_at DESC`func (q *Queries) ListCategories(ctx context.Context) ([]Category, error) {rows, err := q.db.Query(ctx, listCategories)if err != nil {return nil, err}defer rows.Close()var items []Categoryfor rows.Next() {var i Categoryif err := rows.Scan(&i.ID,&i.Name,&i.ParentID,&i.SpecTemplate,&i.Sort,&i.CreatedAt,); err != nil {return nil, err}items = append(items, i)}if err := rows.Err(); err != nil {return nil, err}return items, nil}
        {            "SpecTemplate": "eyLlhoXlrZgiOiBbIjRHQiIsICI2R0IiLCAiOEdCIiwgIjEyR0IiLCAiMTZHQiJdLCAi5a2Y5YKoIjogWyI2NEdCIiwgIjEyOEdCIiwgIjI1NkdCIiwgIjUxMkdCIiwgIjFUQiJdLCAi6aKc6ImyIjogWyLpu5HoibIiLCAi55m96ImyIiwgIuiTneiJsiIsICLnuqLoibIiLCAi6YeR6ImyIl19",        },
You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
1 participant
@sanbei101

[8]ページ先頭

©2009-2025 Movatter.jp