GORM Playground Link

https://github.com/go-gorm/playground/pull/444

Description

Runing a SELECT SUM(column) query over a table returns wrong results.

For a table with a column amount and five rows that each have an amount of 17.99 as a DECIMAL(20,8), SELECT SUM(amount) should equal 89.95. However, for sqlite, it equals 89.94999999999999.

I am not 100% sure if this is a bug in shopspring/decimal or gorm, but as sqlite is affected and postgresql not, I suspect gorm.

For full details, see the GORM Playground reproduction above.

Comment From: Heliner

this look like sqllite cann't support decimal by postgresql can.

Comment From: Heliner

sqllite : https://stackoverflow.com/questions/44298684/sqlite-not-storing-decimals-correctly postgresql : https://www.postgresql.org/docs/9.1/datatype-numeric.html

Comment From: morremeyer

In the playground reproduction, I showed that the result the SQLite database returns is correct.

SQLite supports decimals with the numeric affinity and the data Type is set correctly.

Comment From: jinzhu

I am not 100% sure if this is a bug in shopspring/decimal or gorm, but as sqlite is affected and postgresql not, I suspect gorm.

As you are using Row().Scan, it is just using the generic sql.Row to decode the db value to shopspring/decimal, and GORM do nothing for the decoding data part, so I guess this is caused by the sqlite or the sqlite driver doesn't handle the decimals correctly.

Closing it because GORM can do nothing for it.

Sorry for that.