Golang Gorm Sum decimal 字段类型

发布时间: 2022-03-18 14:08:35 作者: 大象笔记

感觉是 Gorm 的 Bug,在使用 Pluck 获取 Sum 结果时总是报错。 而用 Scan 就没有问题。

可行的做法 Scan

type FHSum struct {
	Total float64
}

var total FHSum
db.Select("sum(money) as total").Scan(&total)

这样能获取到 sum 的结果。

Pluck float64 报错

var total []float64		// slice 的第一个数字就是 sum 值
db.Select("sum(money) as total").Pluck("total", &total)

执行时报错:

reflect: call of reflect.Value.Field on float64 Value

但是对于 int 字段,用这种方式一直没问题。

float32 执行时报错

reflect: call of reflect.Value.Field on float32 Value

int 执行时报错

sql: Scan error on column index 0, name "total": converting driver.Value type []uint8 ("1001.23") to a int: invalid syntax

uint8 执行时报错

sql: Scan error on column index 0, name "total": converting driver.Value type []uint8 ("1001.23") to a uint8: value out of range

我是一名山东烟台的开发者,联系作者