From c33337f5cca3519509cfd0dbb96414643b2e8369 Mon Sep 17 00:00:00 2001 From: yinheli Date: Sat, 24 Jul 2021 00:34:01 +0800 Subject: [PATCH] update model FindOne --- tools/xservice/model/mysql/table.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/xservice/model/mysql/table.go b/tools/xservice/model/mysql/table.go index 5f1a387..4770b20 100644 --- a/tools/xservice/model/mysql/table.go +++ b/tools/xservice/model/mysql/table.go @@ -488,10 +488,13 @@ func (t *MySQLGenerator) tableDefaultModel(table *Table) *jen.Statement { jen.Return(jen.Id("&data, err")), ).Line() - c.Comment("FindOne, use find to avoid 'record not found' error").Line() + c.Comment("FindOne, avoid 'record not found' error").Line() c.Func().Params(jen.Id("t").Op("*").Id(typeName)).Id("FindOne").Params(jen.Id("conds ...interface{}")).Op("(*").Id(modelName).Id(",error").Op(")").Block( jen.Id("var data ").Id(modelName), - jen.Id("err := t.tx.Limit(1).Find(&data, conds...).Error"), + jen.Id("err := t.tx.First(&data, conds...).Error"), + jen.If(jen.Id("err != nil &&").Qual("errors", "Is(err, gorm.ErrRecordNotFound)")).Block( + jen.Return(jen.Id("nil, err")), + ), jen.Return(jen.Id("&data, err")), ).Line()