我有一个类似下面的gorm结构。
type Book1 struct {
ID uint `gorm:"primary_key"`
Title string
Price decimal.Decimal `sql:"type:decimal(20,8);"` //github.com/shopspring/decimal
}
我可以使用db.AutoMigrate(&Book1{})
在postgres
中创建表格,但可以将价格类型作为数字类型。在sqllite
上,它正在正确创建"price" decimal(20,8)
字段。
但是当我尝试添加以下条目
b := Book1{
ID: 10,Title: "hello",Price: decimal.NewFromFloat(10.2),}
er := db.Debug().Create(&b).Error
在postgres上会产生错误encode: unknown type for decimal.Decimal
但在sqlite上,它会为价格添加空值。
调试日志打印INSERT INTO "book1" ("id","title","price") VALUES (10,'hello','10.2')