【发布时间】:2021-08-30 12:33:18
【问题描述】:
我使用 GORM 运行以下代码:
result := db.Where("account_name = ?", config.AccountName).First(&accountRecord) // line 299
// if there is some error when working with DB
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
fmt.Println("error when processing accountResponse")
return 0, false, result.Error
}
// in case that record exists
if result.Error != nil {
accountRecord = newAcountData
result = db.Create(&accountRecord)
} else {
result = db.Model(&accountRecord).Updates(newAcountData)
}
我已经想在找不到记录的情况下运行一些逻辑。但是 - 错误仍然显示在我的控制台中,说 The record was not found:
2021/08/30 18:30:16 /Users/kana/projects/server/data-processor/commands.go:289 record not found
[4.966ms] [rows:0] SELECT * FROM "accounts" WHERE account_name = 'kana' AND "accounts"."deleted_at" IS NULL ORDER BY "accounts"."id" LIMIT 1
为什么会这样?之后整个程序运行良好 - 没有崩溃,整体行为是它的设计方式。只是这个错误消息让我很烦。
【问题讨论】: