Gorm: get all 0 when gets decimal field value from mysql

Created on 10 May 2018  ·  3Comments  ·  Source: go-gorm/gorm

when i use gorm to gets decimal field value from mysql,whatever this struct dield type is float32,float64 or shopspring/decimal,it gets all 0

What version of Go are you using (go version)?

go version go1.8.3 linux/amd64

Which database and its version are you using?

mysql 5.6.23

Models:

package main

import (
    "log"
    "github.com/shopspring/decimal"
)

func main() {
    month := "201805"
    t := "2018-05-07"
    companyUser, _ := CompanyUserRepo.GetDaily(month, t)
    for _, cu := range companyUser {
        log.Println(cu.Ks3Storage)
    }
}

type CompanyUser struct {
    Ks3Storage float64  // or float32 or decimal.Decimal
}

var CompanyUserRepo = new(CompanyUser)

func (*CompanyUser) GetDaily(month string,
    date string) ([]*CompanyUser, error) {

    var cs []*CompanyUser
    table := "company_user_201805"
    err := db.LogMode(true).Table(table).Where("stat_date=?",
        date).Find(&cs).Error
    if err != nil {
        return nil, err
    }
    return cs, nil
}

then output is always 0,whatever the real value is.

What am I doing wrong here ? Or how I should do ?

gorm_v1

Most helpful comment

@jinzhu Confirm that it would be nice and useful having this support!

All 3 comments

@jinzhu ,please!

@jinzhu Confirm that it would be nice and useful having this support!

This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you

Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alanyuen picture alanyuen  ·  3Comments

RadhikaBhat picture RadhikaBhat  ·  3Comments

littletwolee picture littletwolee  ·  3Comments

superwf picture superwf  ·  3Comments

kumarsiva07 picture kumarsiva07  ·  3Comments