Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wenjian022 opened this issue Apr 26, 2024 · 5 comments
Assignees
Labels

Comments

@wenjian022
Copy link

wenjian022 commented Apr 26, 2024

查询代码

	// 关键字查询,服务器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 github-actions bot added the type:missing reproduction steps missing reproduction steps label Apr 26, 2024
Copy link

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
Copy link
Contributor

philhuan commented May 6, 2024

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

Copy link

github-actions bot commented May 6, 2024

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
Copy link
Author

wenjian022 commented May 6, 2024

发一下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)
}

Copy link

github-actions bot commented May 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants