在gorm中,列索引0的扫描错误,名称为xxxxx:目标不是指针。

huangapple go评论152阅读模式
英文:

Scan error on column index 0 , name xxxxx: destination not a pointer" gorm

问题

我遇到了这个错误 sql: Scan error on column index 0, name "step": destination not a pointer,使用gorm。

这是我的代码:

results := []interface{}{}
if err := postgres.Db.Table("states").Select("\"step\",\"sequence\",task_name,state,TO_CHAR(time_start,'YYYY-MM-DD HH:MM:SS') time_start,TO_CHAR(time_end,'YYYY-MM-DD HH:MM:SS') time_end").Where("bugreport_id=?", id).Order("id").Find(&results).Error; err != nil {
	log.Error(err)
	return
}

查询语句正常运行:SELECT "step","sequence",task_name,state,TO_CHAR(time_start,'YYYY-MM-DD HH:MM:SS') time_start,TO_CHAR(time_end,'YYYY-MM-DD HH:MM:SS') time_end FROM "states" WHERE bugreport_id=1 ORDER BY id

但是如果我使用以下代码,它就可以正常工作:

results:=map[string]interface{}{}

谢谢。

英文:

I am getting this error sql: Scan error on column index 0, name "step": destination not a pointer with gorm.

This is my code:

results := []interface{}{}
if err := postgres.Db.Table("states").Select("\"step\",\"sequence\",task_name,state,TO_CHAR(time_start,'YYYY-MM-DD HH:MM:SS') time_start,TO_CHAR(time_end,'YYYY-MM-DD HH:MM:SS') time_end").Where("bugreport_id=?", id).Order("id").Find(&results).Error; err != nil {
	log.Error(err)
	return
}

The query works fine: SELECT "step","sequence",task_name,state,TO_CHAR(time_start,'YYYY-MM-DD HH:MM:SS') time_start,TO_CHAR(time_end,'YYYY-MM-DD HH:MM:SS') time_end FROM "states" WHERE bugreport_id=1 ORDER BY id

在gorm中,列索引0的扫描错误,名称为xxxxx:目标不是指针。

but it works fine if I use

results:=map[string]interface{}{}

Thank you

答案1

得分: 1

Find()方法期望传入一个指向数据的指针切片,并且需要一种键-值引用来存储结果,但是你提供了[]interface{}{},这是一个空的空接口切片,无法以键-值的形式存储数据。它没有具体的类型,无法被GORM用来填充结果。

为什么map[string]interface{}{}可以工作?

这里使用了一个带有string键和interface{}值的映射,它允许检索和存储结果。通过这种方式,GORM将列名作为键,而interface{}类型的值可以容纳任何类型的值。

我建议使用一个结构体切片,这样可以保证类型安全,并且方便处理检索到的数据。

type States struct{
 Step string 
 Sequence int
 ....
}

// 然后像这样使用
results := []States{}
英文:

Find() method expects a slice of pointers to populate the data, and it needs a kind of key-value reference to store the results, but you provided []interface{}{} which is an empty slice of empty interfaces and can't able to store the data as in the form of key-value. It doesn't have a concrete type which the GORM to populate the results.

Why map[string]interface{}{} worked ?

This is using a map with string keys and interface{} as value and it allows to retrieve and store the results. With this, GORM populate the column name as key and the value which is interface{} can hold any kind of value.

I'd suggest to use a slice of struct which can be type safe and easy to work with retrieved data.

type States struct{
 Step string 
 Sequence int
 ....
}

// then use like
results := []States{}

huangapple
  • 本文由 发表于 2023年6月22日 10:37:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定