go-gorm / gorm

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

使用gorm生成的sql能查询到数据,但是返回的结构体里面没有数据 #6995

Closed wenjian022 closed 5 months ago

wenjian022 commented 5 months ago

查询代码

    // 关键字查询,服务器IP/名称/实例ID
    if receiver.Keywords != "" {
        hostDb = hostDb.Where("server_name like ? or ip like ? or instance_id like ? or prisons like ?", "%"+receiver.Keywords+"%", "%"+receiver.Keywords+"%", "%"+receiver.Keywords+"%", "%"+receiver.Keywords+"%")
    }

    // 统计
    if err := hostDb.Count(&res.TotalCount).Error; err != nil {
        Logger.Logger.Errorln(err)
        return res, err
    }

gorm 生成的sql

[1.423ms] [rows:1] SELECT count(*) FROM `server_hosts` WHERE server_name like "%浙江%" or ip like "%浙江%" or instance_id like "%浙江%" or prisons like "%浙江
%"

获取到的结果

image

通过数据库查获取的结果

image

prisons 字段是自定义类型,我不知道是不是自定义字段导致查不出来的 ,把prisons 条件删了就正常查

数据源是sqlite

github-actions[bot] commented 5 months 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

philhuan commented 5 months ago

发一下model 结构体,特别是这个自定义类型,以及这个字段的sql定义 @wenjian022

github-actions[bot] commented 5 months 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

wenjian022 commented 5 months ago

发一下model结构体,特别是这个自定义类型,以及这个字段的sql定义@wenjian022

现在我换了个方式,dll中是 text

type ServerHosts struct {
    Prisons                 []string `gorm:"serializer:json"`
}

之前的,dll中好像是blob, 之后我把他改成了text也不行

type PrisonsList []string

type ServerHosts struct {
    Prisons     PrisonsList
}

func (t *PrisonsList) Scan(value interface{}) error {
    bytesValue, _ := value.([]byte)
    return json.Unmarshal(bytesValue, t)
}

func (t PrisonsList) Value() (driver.Value, error) {
    return json.Marshal(t)
}
github-actions[bot] commented 5 months 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