gogf / gf

GoFrame is a modular, powerful, high-performance and enterprise-class application development framework of Golang.
https://goframe.org
MIT License
11.8k stars 1.61k forks source link

database/gdb: Inconsistency in the err returned when no data is queried during the query #3552

Closed wln32 closed 2 months ago

wln32 commented 7 months ago

Go version

1.22

GoFrame version

2.7.0

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

1.当Scan的参数是指针的时候,也就是*Result,没有查询到数据,返回sql.ErrNoRows

var results Result
q := g.DB("default").Model("results").Where("1!=1")
err := q.Scan(&results)
fmt.Println("err=", err)
  1. 当Scan的参数是二级指针的时候,也就是**Result,没有查询到数据,没有返回sql.ErrNoRows
    var results *Result
    q := g.DB("default").Model("results").Where("1!=1")
    err := q.Scan(&results)
    fmt.Println("err=", err)

3.当Scan的参数是[]Result或者[]*Result的时候,没有查询到数据,没有返回sql.ErrNoRows

var results []*Result
q := g.DB("default").Model("results").Where("1!=1")
err := q.Scan(&results)
fmt.Println("err=", err)

建议统一返回错误,或者不返回错误比较好点

What did you see happen?

err= sql: no rows in result set err= nil err= nil

What did you expect to see?

err= nil err= nil err= nil

或者 err= sql: no rows in result set err= sql: no rows in result set err= sql: no rows in result set

gqcn commented 2 months ago

@wln32 这块设计如此哈,主要是为了避免很多没有意义的if err != nil && err != sql.ErrNoRows的冗余判断代码,具体请参考文档:https://goframe.org/pages/viewpage.action?pageId=47699230

Issues-translate-bot commented 2 months ago

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@wln32 This piece of design is so hazy, mainly to avoid a lot of meaningless redundant judgment codes of if err != nil && err != sql.ErrNoRows. For details, please refer to the document: https://goframe.org/pages /viewpage.action?pageId=47699230