You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using sqlc with PostgreSQL and pgx/v5, columns of type JSONB are generated as []byte in the Go model. This causes standard JSON marshaling (e.g., via Gin or encoding/json) to encode the field as a Base64 string instead of the expected raw JSON object.
For example, a spec_template JSONB NOT NULL column becomes:
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 :many SELECT 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 []Category for rows.Next() { var i Category if 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 }
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Problem Description
When using
sqlcwith PostgreSQL andpgx/v5, columns of typeJSONBare generated as[]bytein the Go model. This causes standard JSON marshaling (e.g., via Gin orencoding/json) to encode the field as a Base64 string instead of the expected raw JSON object.For example, a
spec_template JSONB NOT NULLcolumn becomes:When serialized:
how should i do?
here is all code:
Beta Was this translation helpful? Give feedback.
All reactions