Reputation: 3971
I have the following code running using 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)
}
I already do want to run some logic in the case where the record was not found. However - the error is still shown in my console saying 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
Why does that happen? The whole program runs fine after that - nothing crashes and the overall behavior is the way it was designed. Simply this error message annoys me.
Upvotes: 0
Views: 584
Reputation: 102
This is because the GORM is handling some log outputs and record not found
is one of them. If you need to disable these kind of log activities you can do the followings:
if you are using GORM v1
https://pkg.go.dev/github.com/jinzhu/gorm#DB.LogMode
db.LogMode(false)
if you are using GORM v2 you can pass logger to gorm config and control the outputs.
https://gorm.io/docs/logger.html#Log-Levels
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
Upvotes: 2