gorm stdErr = sql: Scan error on column index 0, name "total": converting NULL to float64 is unsupported

发布时间 2023-09-13 23:15:36作者: 牛奔

前言

使用 gorm 查询时,报错:stdErr = sql: Scan error on column index 0, name "total": converting NULL to float64 is unsupported

代码如下

var total float64
res := db.Model(&model.Record{}).Select("sum(amount) as total").Where("id = ? and type = ? ", key.Id, key.Type).Find(&total)
if res.Error != nil {
return 0, errors.Errorf(res.Error, errors.ErrorDBFindFailed)
}

这个错误是因为查询结果中的 total 列的值为 NULL,而尝试将 NULL 转换为 float64 类型时发生了错误。

查询结果中的 total 列的值为 NULL 可能有多种原因:

  1. 数据库中没有匹配的记录:根据查询条件,可能没有符合条件的记录存在,因此返回的"total"列的值为 NULL。这意味着在数据库中没有与给定的 idtype 匹配的记录。
  2. amount 列的值为 NULL:如果 amount 列的值为 NULL,那么使用 SUM 函数计算总和时,结果将为 NULL。因此,total 列的值将为 NULL
  3. 数据库中存在 NULL值:如果 amount 列中存在 NULL 值,并且在查询中没有处理 NULL 值的情况,那么计算总和时将返回 NULL

解决方法

import "database/sql"

var total sql.NullFloat64
res := db.Model(&model.Record{}).Select("COALESCE(sum(amount), 0) as total").Where("id = ? and type = ? ", key.Id, key.Type).Find(&total)
if res.Error != nil {
    return 0, errors.Errorf(res.Error, errors.ErrorDBFindFailed)
}

if total.Valid {
    return total.Float64, nil
} else {
    return 0, nil 
}

在上述代码中,使用了 SQL 的 COALESCE 函数将 sum(amount) 的结果替换为0(或其他默认值)。

然后,我们检查 total.Valid 字段,如果查询结果不为 NULL ,则将其转换为 float64 类型并返回。

通过这种方式,即使查询结果中的"total"列的值为NULL,也可以正常处理,并且可以返回默认值或根据业务逻辑进行处理。