-
Notifications
You must be signed in to change notification settings - Fork 923
Emit ErrNoRows in many queries #3787
Unanswered
lawmatsuyama
asked this question in
Q&A
-
Hi!
I have the following query that I want to generate with sqlc:
-- name: GetAccounts :many select * from account where type = $1;
The code generated by sqlc does not return an error like ErrNoRows when the query returns no rows. Is it possible to force it to return ErrNoRows when there are no records?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
For PostgreSQL, a :many
query will emit the following code
func (q *Queries) ListAuthors(ctx context.Context) ([]Author, error) {
rows, err := q.db.QueryContext(ctx, listAuthors)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Author
for rows.Next() {
var i Author
if err := rows.Scan(&i.ID, &i.Name, &i.Bio); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
This will return a ErrNoRows from pgx or database/sql. What database are you using? Can you post the generated code for your query?
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment