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

前言

使用 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,也可以正常处理,并且可以返回默认值或根据业务逻辑进行处理。

posted @   牛奔  阅读(355)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2020-09-13 mysql [Err] 1067 - Invalid default value for
2020-09-13 mysql [ERR] 1273 - Unknown collation: 'utf8mb4_0900_ai_ci'
2020-09-13 laradock 更改 mysql 版本
点击右上角即可分享
微信分享提示