// for trade_service
type AmountConfig struct {
MaxCny float64 `thrift:"max_cny,1" json:"max_cny"`
MaxBtc float64 `thrift:"max_btc,2" json:"max_btc"`
}
//
type AmountConfig struct {
gorm.Model
trade_service.AmountConfig //
}
func getAmountConfig() (amount_config *trade_service.AmountConfig, err error) {
db, err := getORMDB()
if err != nil {
logger.Errorln(err)
return
}
var amountConfig AmountConfig{}
if err = db.First(&amountConfig).Error; err != nil {
logger.Errorln("getAmountConfig amount record does not exist:", err)
return
}
amount_config = trade_service.NewAmountConfig()
amount_config.MaxCny = amountConfig.MaxCny
amount_config.MaxBtc = amountConfig.MaxBtc
logger.Infoln("get amountConfig ok", amount_config)
return
}
mysql表描述如下:
CREATE TABLE `amount_config` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`max_cny` decimal(65,2) DEFAULT NULL,
`max_btc` decimal(65,4) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`deleted_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `updated_at` (`updated_at`),
KEY `created_at` (`created_at`),
KEY `deleted_at` (`deleted_at`)
ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
我在talbe "amount_config“中有一条记录,但在我运行代码”db.First(&found)“后,错误显示"record not found”。
我的代码有什么问题吗?
发布于 2017-03-27 22:11:30
没有实际数据很难判断,但我想说最有可能的是精度和舍入。您的数据类型是double,并且很可能会作为sql类型double发送到mysql。为了进行比较,它将被转换为数字,但它的精度将在逗号后的15-18位左右,并且可能与您在代码中指定的值不完全相同(例如,0.1可能会变成类似0.10000000000005的值)。
您可以通过为这些字段添加显式gorm映射来修复此问题(有关确切格式,请参阅gorm文档)。但我更倾向于建议使用github.com/shopspring/decimal
,这样您就可以始终知道您正在使用的精确程度。显式地将这些字段映射为数字仍然是一个好主意。
发布于 2020-01-21 11:58:33
这是gorm的一个已知的故意问题。当err:= db.Preload("UserDevices").First(&user, userID.ID).Error
返回的列表为空时,返回错误record not found
。有关更多详细信息,请查看此github issue
https://stackoverflow.com/questions/43020899
复制相似问题