Skip to content

Return affectedRows after executing DELETE statement #2808

Answered by kyleconroy
KarlsMaranjs asked this question in Q&A
Discussion options

You must be logged in to vote

You're in luck, as we already have this option. Instead of using :exec, use :execrows.

CREATE TABLE authors (
  id   BIGSERIAL PRIMARY KEY,
  name text      NOT NULL,
  bio  text
);

-- name: DeleteAuthor :execrows
DELETE FROM authors
WHERE id = $1;

which generates the code you're looking for

func (q *Queries) DeleteAuthor(ctx context.Context, id int64) (int64, error) {
	result, err := q.db.ExecContext(ctx, deleteAuthor, id)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@KarlsMaranjs
Comment options

Answer selected by kyleconroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants