Scan error on column index 0, name \"ID\": unsupported Scan, storing driver.Value type int64 into type *[]authService.Permission"

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

Scan error on column index 0, name \"ID\": unsupported Scan, storing driver.Value type int64 into type *[]authService.Permission"

问题

当我尝试调用err = row.Scan(&resourceList, resourceTypeId)时,我遇到了以下错误:

> 在列索引0上扫描错误,名称为"ID":不支持的扫描,将driver.Value类型int64存储到类型*[]authService.Permission中"

这里有什么问题?谢谢。

英文:

I am getting the below error when I try to call err = row.Scan(&resourceList, resourceTypeId)

> Scan error on column index 0, name "ID": unsupported Scan, storing driver.Value type int64 into type *[]authService.Permission"

type Permission struct {
    ID               int    `json:"id"`
    Name             string `json:"name"`
    Description      string `json:"description"`
    ParentResourceID int    `json:"parentResourceId"`
}

func GetResourcesByResourceTypeId(resourceTypeId string) ([]Permission, string, error) {
db, ctx := db.GetDB()
query := "CALL usp_GetParentResourceListByResourceTypeID(?)"
var resourceList []Permission
stmt, err := db.Prepare(query)
defer stmt.Close()
if err != nil {
	log.Errorln("Error in preparing statement. " + err.Error())
	return nil, "Error in preparing statement.", err
}

row := stmt.QueryRowContext(ctx, resourceTypeId)

err = row.Scan(&resourceList, resourceTypeId)
if err == nil {
	return resourceList, "Resource retrieval.", nil
}

log.Warningln("Resource retrieval failed, ResourceTypeID: " + resourceTypeId + ".")
return resourceList, "Resource retrieval failed.", nil
}

SQL Returns below

ID  Name
15  Applications
16  Subscriptions
17  Payments

The same query works fine when I try to use SQL Server with EXEC statement in the query.

Any idea on whats wrong here? Thanks in advance.

答案1

得分: 2

这里有几个问题。首先,QueryRowContext执行的查询预期最多返回一行结果。根据你的问题描述,你的语句返回多个结果,所以这不是正确的函数(更适合使用QueryContext)。

第二个问题是错误信息中所述的:

> 不支持的 Scan,将 driver.Value 类型 int64 存储到类型 *[]authService.Permission"

结果集中的第一列是一个整数(可能是值15),你试图将其扫描到[]Permission类型中。如果你将var resourceList []Permission改为var resourceList int,这个错误就会被修复(但第二个参数也需要修改)。

请参考文档中的这个示例。将该示例代码应用到你的情况中,会得到类似以下的代码(未经测试,只是为了指导你正确的方向):

rows, err := db.QueryContext(ctx, "CALL usp_GetParentResourceListByResourceTypeID(?)", resourceTypeId)
if err != nil {
	log.Fatal(err)
}
defer rows.Close()

var resourceList []Permission

for rows.Next() {
	var resource Permission
	if err := rows.Scan(&resource.ID, &resource.Name); err != nil {
		// 检查扫描错误。
		// 查询行将在 defer 中关闭。
		log.Fatal(err)
	}
	resourceList = append(resourceList, resource)
}

rerr := rows.Close()
if rerr != nil {
	log.Fatal(rerr)
}

if err := rows.Err(); err != nil {
	log.Fatal(err)
}

**注意:**你的结构体Permission包含四个元素,但查询返回两列,所以我不确定你打算如何填充其他两个元素(或者它们的映射关系是什么)。

英文:

There are a few issues here. Firstly QueryRowContext

>executes a query that is expected to return at most one row

Your question indicates that your statement returns multiple results so this is not the right function to use (QueryContext would be more appropriate).

The second issue is as stated in the error:

> unsupported Scan, storing driver.Value type int64 into type *[]authService.Permission"

The first column in the result set is an integer (probably the value 15 in this case) and you are trying to scan that into a []Permission. If you changed
var resourceList []Permission to var resourceList int that error would be fixed (but the second parameter also needs work).

Take a look at this example in the documentation. Taking that code and applying it to your situation will result in something like the following (untested; just intended to point you in the right direction):

rows, err := db.QueryContext(ctx, "CALL usp_GetParentResourceListByResourceTypeID(?)", resourceTypeId)
if err != nil {
	log.Fatal(err)
}
defer rows.Close()

var resourceList []Permission

for rows.Next() {
	var resource Permission
	if err := rows.Scan(&resource.ID, &resource.Name); err != nil {
		// Check for a scan error.
		// Query rows will be closed with defer.
		log.Fatal(err)
	}
	resourceList = append(resourceList, resource )
}

rerr := rows.Close()
if rerr != nil {
	log.Fatal(rerr)
}

if err := rows.Err(); err != nil {
	log.Fatal(err)
}

Note: Your structure Permission contains four elements but the query returns two columns so I'm not really sure how you intend to fill the other two (or what the mapping is).

huangapple
  • 本文由 发表于 2022年8月2日 04:10:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/73199081.html
匿名

发表评论

匿名网友

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

确定