go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.94k stars 3.93k forks source link

不确认是否是一个BUG,关于JSONB类型存储时,被自动base64加密(加密后也无法正常反溯) #6270

Closed millim closed 1 year ago

millim commented 1 year ago

版本

gorm.io/gorm v1.24.5 中出现问题 gorm.io/gorm v1.21.12 中正常

环境

使用postgresql 15版本

有如下JSONB类型

type StringArray []byte

func (j *StringArray) Scan(value any) error {
    bytes, ok := value.([]byte)
    if !ok {
        return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
    }
    result := json.RawMessage{}
    err := json.Unmarshal(bytes, &result)
    *j = StringArray(result)
    return err
}

func (j StringArray) Value() (driver.Value, error) {
    if len(j) == 0 {
        return nil, nil
    }
    return json.RawMessage(j).MarshalJSON()
}

func (j StringArray) Get() ([]string, error) {
    var ss []string
    if len(j) == 0 {
        return ss, nil
    }
    b, err := json.RawMessage(j).MarshalJSON()
    if err != nil {
        return ss, err
    }
    err = json.Unmarshal(b, &ss)
    return ss, err
}

func (j *StringArray) Set(value []string) error {
    d, err := json.Marshal(value)
    if err != nil {
        return err
    }
    *j = d
    return nil
}

定义如下类型

type Book struct {
     Name string `gorm:"name"`
    //数据库类型为JSONB
     Numbers StringArray `gorm:"numbers"`
}

问题描述

在新版本中,在 createsave 时,日志打印的sql语句正常,StringArray是带数组的内容,但是在数据库中保存的是带 " 号,并使用base64加密后的内容,同时由于 " 号的存在,数据无法正常读取,而在退回老版本后,此问题解决。

其中,日志打印的SQL语句直接复制到命令台输入,也是正确的。

github-actions[bot] commented 1 year ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking