Gorm: decimal insert or update error;How to solve?

Created on 17 Sep 2019  路  6Comments  路  Source: go-gorm/gorm

decimal package "github.com/shopspring/decimal"

type CircleMoney struct {
    Id          int             `gorm:"primary_key" json:"id"`
    UserId      int             `gorm:"user_id" json:"user_id"`
    KeyId       int             `gorm:"key_id" json:"key_id"`
    Platform    string          `gorm:"platform" json:"platform"`
    Money       decimal.Decimal `gorm:"money" json:"money"`
    Currency    string          `gorm:"currency" json:"currency"`         
    ConvertUsdt decimal.Decimal `gorm:"convert_usdt" json:"convert_usdt"` 
    CreateTime  int64           `gorm:"create_time" json:"create_time"`   
    UpdateTime  int64           `gorm:"update_time" json:"update_time"`  
}

insert into :

balance, _ := decimal.NewFromString("123.456")
info = CircleMoney{
    UserId:      user_id,
    KeyId:       key_id,
    Platform:    "xxx",
    Money:       balance,
    Currency:    currency,
    ConvertUsdt: balance.Mul(rate),
    CreateTime:  time.Now().Unix(),
    UpdateTime:  time.Now().Unix(),
}
db.Create(&info)

error:
cannot convert type: decimal.Decimal

gorm_v1

Most helpful comment

go version 1.13 it's error
go version 1.11 it's ok
please fixed
@jinzhu

All 6 comments

go version 1.13 it's error
go version 1.11 it's ok
please fixed
@jinzhu

That was because of new decimal handling in 1.13 https://golang.org/cl/174181

For those who want a workaround - use older version of github.com/shopspring/decimal with 1.13 that does not implement new interface.

go mod edit -require github.com/shopspring/decimal@cd690d0c9e2447b1ef2a129a6b7b49077da89b8e
go get -v -t ./...

The same is not possible
I using v1.1.0 version

We are having the same problem, downgrading is never a good option, can you take a look at this @jinzhu ?

@vviotto will look at it in v2

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

zeropool picture zeropool  路  3Comments

Quentin-M picture Quentin-M  路  3Comments

hypertornado picture hypertornado  路  3Comments

sredxny picture sredxny  路  3Comments

Ganitzsh picture Ganitzsh  路  3Comments