Pass through no-rows errors to the user for .One()

This commit is contained in:
Aaron L 2016-08-13 11:42:28 -07:00
parent 1ac3853d3c
commit 956157d057
3 changed files with 11 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package boil
import (
"database/sql"
"fmt"
"reflect"
"strings"
@ -101,7 +102,9 @@ func bind(q *Query, obj interface{}, structType, sliceType reflect.Type, singula
ptrSlice = reflect.Indirect(reflect.ValueOf(obj))
}
foundOne := false
for rows.Next() {
foundOne = true
var newStruct reflect.Value
var pointers []interface{}
@ -124,6 +127,10 @@ func bind(q *Query, obj interface{}, structType, sliceType reflect.Type, singula
}
}
if singular && !foundOne {
return sql.ErrNoRows
}
return nil
}

View file

@ -145,6 +145,7 @@ var defaultTemplateImports = imports{
standard: importList{
`"fmt"`,
`"strings"`,
`"database/sql"`,
},
thirdParty: importList{
`"github.com/pkg/errors"`,

View file

@ -18,6 +18,9 @@ func (q {{$varNameSingular}}Query) One() (*{{$tableNameSingular}}, error) {
err := q.Bind(o)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, sql.ErrNoRows
}
return nil, errors.Wrap(err, "{{.PkgName}}: failed to execute a one query for {{.Table.Name}}")
}