georgysavva / scany

Library for scanning data from a database into Go structs and more
MIT License
1.23k stars 67 forks source link

ErrNoRows not working #113

Closed ggalihpp-jubelio closed 12 months ago

ggalihpp-jubelio commented 1 year ago

Hi, great lib! Thanks for this...

however, I have difficulties to identified a no rows error. I'm using pgx/v5 and here's the code looks like

    query := `SELECT username, picture FROM user_dasi WHERE username = $1 LIMIT 1`

    err = pgxscan.Get(ctx, r.Db, &user, query, username)

    if err == pgx.ErrNoRows {
        fmt.Println("pgx.ErrNoRows")
    }

    if err == sql.ErrNoRows {
        fmt.Println("sql.ErrNoRows")
    }

    return

it have no rows error but never go either to pgx/sql ErrNoRows.

georgysavva commented 1 year ago

Hi! The reason you don't see the error as pgx.ErrNoRows or sql.ErrNoRows is because scany wraps database errors. If you want to check for a "no rows" error, you need to use the standard lib errors package: errors.Is(). It's a common pattern in Go and not something specific to scany. Additionally, scany provides its own no rows check functions: pgxscan.NotFound() and sqlscan.NotFound(). You can use these instead of errorr.Is(). It's the same thing.

Let me know if you have any questions.

ggalihpp-jubelio commented 12 months ago

HI! thanks for replying...

Will check it when I'm getting back to the project... and update it if it is good or not.

Meanwhile will close this.. thanks again