From 949c2d0eba0ebccf22a1bacd421cd96ce8f7ab38 Mon Sep 17 00:00:00 2001 From: xxjwxc Date: Wed, 5 Aug 2020 22:20:27 +0800 Subject: [PATCH] add gorm.ErrRecordNotFound MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加快捷函数未找到处理逻辑 --- data/view/genfunc/def.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/data/view/genfunc/def.go b/data/view/genfunc/def.go index 414b029..eb02659 100644 --- a/data/view/genfunc/def.go +++ b/data/view/genfunc/def.go @@ -188,18 +188,28 @@ func (obj *_{{$obj.StructName}}Mgr) GetBatchFrom{{$oem.ColStructName}}({{CapLowe ` genPreload = `if err == nil && obj.isRelated { {{range $obj := .}}{{if $obj.IsMulti}} - err = obj.DB.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}List).Error // {{$obj.Notes}} - {{else}} - err = obj.DB.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}).Error // {{$obj.Notes}} - {{end}} {{end}}} + if err = obj.DB.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}List).Error;err != nil { // {{$obj.Notes}} + if err != gorm.ErrRecordNotFound { // 没找到 + return + } + } {{else}} + if err = obj.DB.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", result.{{$obj.ColStructName}}).Find(&result.{{$obj.ForeignkeyStructName}}).Error; err != nil { // {{$obj.Notes}} + if err != gorm.ErrRecordNotFound { // 没找到 + return + } + }{{end}} {{end}}} ` genPreloadMulti = `if err == nil && obj.isRelated { for i := 0; i < len(results); i++ { {{range $obj := .}}{{if $obj.IsMulti}} if err = obj.DB.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", results[i].{{$obj.ColStructName}}).Find(&results[i].{{$obj.ForeignkeyStructName}}List).Error;err != nil { // {{$obj.Notes}} - return + if err != gorm.ErrRecordNotFound { // 没找到 + return + } } {{else}} if err = obj.DB.New().Table("{{$obj.ForeignkeyTableName}}").Where("{{$obj.ForeignkeyCol}} = ?", results[i].{{$obj.ColStructName}}).Find(&results[i].{{$obj.ForeignkeyStructName}}).Error; err != nil { // {{$obj.Notes}} - return + if err != gorm.ErrRecordNotFound { // 没找到 + return + } } {{end}} {{end}} } }`