亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 jsonb_agg/jsonb_build_object 解析為內部結構

使用 jsonb_agg/jsonb_build_object 解析為內部結構

Go
一只斗牛犬 2022-08-01 16:54:38
每當我嘗試獲取(選擇/掃描)組(外部結構)及其協作者(內部結構)時,我都會收到以下錯誤:// sql: Scan error on column index ..., name "collaborators": unsupported Scan, storing driver.Value type []uint8 into type *[]User我正在使用sqlx(帶有pgx驅動程序)。從 db 獲取的代碼是:func (psql *Postgres) GetGroups(someParam string) ([]Group, error) {   groups := []Group{}   err := psql.db.Unsafe().Select(&groups, <the query ...>, someParam)   ....}type Postgres struct {    db      *sqlx.DB    config  *config.PostgresDB    timeout time.Duration}這是 SQL 查詢:SELECT groups.id,        groups.title,       JSONB_AGG(JSONB_BUILD_OBJECT(        'id', u.id,        'first_name', u.first_name,         'last_name', u.last_name,        'user_pic_url', u.user_pic_url)) as collaboratorsFROM groupsJOIN user_group_permissions pON   p.group_id = groups.idJOIN users uON   u.id = p.user_id這些是結構:type Group struct {    Id             string  `json:"id" db:"id"`    Title          string  `json:"title"   db:"title"`    Collaborators  []User  `json:"collaborators" db:"collaborators"`}type User struct {    Id            string  `json:"id" db:"id"`    FirstName     string  `json:"first_name" db:"first_name"`    LastName      string  `json:"last_name" db:"last_name"`    ProfilePhoto  *string `json:"profile_photo" db:"user_pic_url"`}我有一個簡單的組表,一個用戶表和表,它代表對組有權限的所有用戶:CREATE TABLE groups (   id    int UNIQUE NOT NULL generated always as identity,   title text)CREATE TABLE users (    id       bigint UNIQUE NOT NULL generated always as identity,    first_name   text NOT NULL,    last_name    text NOT NULL,    user_pic_url text)CREATE TABLE user_group_permissions (   group_id   unsigned_int,   user_id    unsigned_bigint,   permission unsigned_smallint,)CREATE DOMAIN unsigned_smallint AS smallint   CHECK(VALUE >= 0 AND VALUE < 32767);CREATE DOMAIN unsigned_int AS int   CHECK(VALUE >= 0 AND VALUE < 2147483647);CREATE DOMAIN unsigned_bigint AS bigint   CHECK(VALUE >= 0 AND VALUE < 9223372036854775807);
查看完整描述

1 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

import "encoding/json"


type Group struct {

    Id             string   `json:"id" db:"id"`

    Title          string   `json:"title"   db:"title"`

    Collaborators  UserList `json:"collaborators" db:"collaborators"`

}


type User struct {

    Id            string  `json:"id" db:"id"`

    FirstName     string  `json:"first_name" db:"first_name"`

    LastName      string  `json:"last_name" db:"last_name"`

    ProfilePhoto  *string `json:"profile_photo" db:"user_pic_url"`

}


type UserList []User


func (list *UserList) Scan(src interface{}) error {

    var data []byte

    switch v := src.(type) {

    case []byte:

        data = v

    case string:

        data = []byte(v)

    default:

        return nil // or return some error

    }

    return json.Unmarshal(data, list)

}


查看完整回答
反對 回復 2022-08-01
  • 1 回答
  • 0 關注
  • 325 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號