'insert decimal type to clickhouse database , there have error of unexpected type string
I am a beginner, I don't understand why insert decimal type field "money", it echo "money (Decimal(18, 2)): unexpected type string"
func main() {
dsn := "tcp://localhost.39:9000?database=test&read_timeout=10&write_timeout=20"
db, err := gorm.Open(clickhouse.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
},
})
if err != nil {
log.Error(err.Error())
}
err = db.Create(&Orderinfo{
Id: uuid.New().String(),
Money: decimal.New(15001, -3),
Vip: "VIP0",
CreateTime: time.Now().UnixMicro(),
}).Error
if err != nil {
log.Error(err.Error())
}
}
create table if not exists test.orderinfo
(
id String ,
money Decimal64(2),
vip String,
create_time Int64
) engine = MergeTree
partition by (create_time)
order by id;
Solution 1:[1]
Try to convert your decimal to string:
strconv.FormatFloat(decimalValue, 'f', -1, 64)
It works for me with library github.com/ClickHouse/clickhouse-go
Solution 2:[2]
I have the same problem. I found "gorm.io/driver/clickhouse" use the package clickhouse-go. when I check the code in clickhouse-go, I found that clickhouse-go support decimal not well! It will check the data type when data insert into database.If data type of col in db is decimal,the clickhouse-go packgae only accept int32,*int32,int64,*int64,float32,float64,*float32,*float64! Gorm mapping decimal to string and Most db do it by this way. So the problem occurs. I found clickhouse-go/v2 support github.com/shopspring/decimal.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | thrownew |
Solution 2 |