自从用了新版本发现日志里出现大量record not found,作为error打印出来了,但我认为这并不是error,而且statement里提供了一个 RaiseErrorOnNotFound 选项,但是这个选项在First()等查询方法中写死了,也无法通过Clause覆盖。

设置LogLevel为Silent屏蔽了所有错误日志,也不合适。

请教一下大佬,有没有别的办法可以解决呢

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: keepeye

通过plugin解决了

        db.Callback().Query().Before("gorm:query").Register("disable_raise_record_not_found", func(d *gorm.DB) {
        d.Statement.RaiseErrorOnNotFound = false
    })

Comment From: jinzhu

Find 就行

Comment From: qifengzhang007

Find 就行

这个问题应该更新解决一下,first 查询没有记录,报 error 错误,很明显不符合逻辑,导致出现了大量的错误日志

Comment From: jinzhu

Find 就行

这个问题应该更新解决一下,first 查询没有记录,报 error 错误,很明显不符合逻辑,导致出现了大量的错误日志

https://gorm.io/docs/query.html#Retrieving-a-single-object 这个是符合设计规范的

Comment From: qifengzhang007

@jinzhu 请问一下,有更加简便的方式,屏蔽 query 操作 出现的 record not found 错误吗? 大概看了一下 first last take 都有这个问题

Comment From: jinzhu

前面不是说了用 Find 么,更多介绍看文档吧

Comment From: james70s

db.Limit(1).Find(&user) 文档里有说明

Comment From: soxft

目前可以通过 初始化时自定义 Logger 来 关闭 ErrRecordNotFound 错误提示了 https://gorm.io/zh_CN/docs/logger.html

Comment From: dingyaguang117

前面不是说了用 Find 么,更多介绍看文档吧

Find 方式也不太完美,比如我想 Find 一张表:如果有记录查出对象,如果没有记录得到 nil.

但是 Find 传入的参数不允许是 nil, 导致判断不存的情况下,是看对象的 id 是否是零值。

如果有些表根本没有 id 字段呢?这就变得很业务相关了。

Comment From: shrimpPaste

find方式看到了,但是find返回的是一个数组,也就意味着我拿他的数据的第一步要判断数组的长度是否==1,才能进行数组下标访问,明明用first能搞定的事情,说句不好听的这样处理我认为就是脱裤子放屁

Comment From: mouday

目前可以通过 初始化时自定义 Logger 来 关闭 ErrRecordNotFound 错误提示了 https://gorm.io/zh_CN/docs/logger.html

楼上正解,文档确实有这个参数

IgnoreRecordNotFoundError: true