想问一下,当定义指针类型的struct变量,查不到数据,是否不用将变量初始化成 empty struct。empty struct和查不到数据是两个概念。 比如定义 var s *struct Find在查不到数据的时候会返回empty struct并且不报错,这种情况应该是s == nil err == nil 如果定义var s struct Find在查不到数据的时候会返回empty struct并且不报错,这种情况应该是err == ErrRecordNotFound.
盼回复,多谢。
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 30 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: feileb
遇到同样的问题,下面的查询返回空struct并且err=nil var stat DevStat{} err := db.Where("id = ? and time < ?", id, time).Order("time DESC").Limit(1).Find(&stat).Error
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 30 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: a631807682
Only the First/Take/Last api will return ErrRecordNotFound as they obviously only return one data.
You can set to return this error if you really need to.
tx := DB.Session(&gorm.Session{})
tx.Statement.RaiseErrorOnNotFound = true
tx.Find()
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 30 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: udoless
同样的问题,很不方便。这是个很细节但很重要的点,也容易引起bug的一个点。
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 30 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: feileb
https://segmentfault.com/a/1190000021363996
err := db.Model(user).Where("id = ?", 1).First(&users).Error
fmt.Println(err, user)
err = db.Model(user).Where("id = ?", 1).Find(user).Error
fmt.Println(err, user)
<nil> &{0 }
record not found &{0 }
传入接收结果集的变量只能为Struct类型或Slice类型,当传入变量为Struc类型时,如果检索出来的数据为0条,会抛出ErrRecordNotFound错误,当传入变量为Slice类型时,任何条件下均不会抛出ErrRecordNotFound错误
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 30 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 ✨