Skip to content

Commit

Permalink
update model FindOne
Browse files Browse the repository at this point in the history
  • Loading branch information
yinheli committed Jul 23, 2021
1 parent a3ccc46 commit c33337f
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions tools/xservice/model/mysql/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit c33337f

Please sign in to comment.