Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Emit ErrNoRows in many queries #3787

Unanswered
lawmatsuyama asked this question in Q&A
Discussion options

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?

You must be logged in to vote

Replies: 1 comment

Comment options

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?

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /