rel icon indicating copy to clipboard operation
rel copied to clipboard

Aggregate does not allow to get a float (decimal) value

Open skolodyazhnyy opened this issue 2 years ago • 2 comments

Aggregate API returns an integer, how do I aggregate a float field?

For instance, I have a decimal price field in my database and I would like to find a sum of all prices:

total, err := repo.Aggregate(ctx, rel.From("products"), "SUM", "price")

Instead of getting a sum I'm getting an error

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

Field price has type DECIMAL in MySQL database.

skolodyazhnyy avatar Aug 23 '23 22:08 skolodyazhnyy

maybe you can use raw query as workaround?

var result struct {Sum float64}
sql := rel.SQL("SELECT ....")
err := repo.Find(ctx, &result, sql)

Fs02 avatar Aug 26 '23 01:08 Fs02

Not really, because result requires primary ID 😄

Finally, after https://github.com/go-rel/primaryreplica/pull/26 got merged, I could make request using sql connection directly. Still would be nice to be able to aggregate on decimal fields.

skolodyazhnyy avatar Aug 26 '23 21:08 skolodyazhnyy