Skip to content

'SELECT EXISTS' queries should return bool, but doesn't #410

Closed
@capnspacehook

Description

@capnspacehook

I want to query for a record in a table using the EXISTS operator (https://www.postgresql.org/docs/11/functions-subquery.html) which returns a boolean in Postgres. But, the generated code by sqlc returns an (interface{}, error). I think a bool return type can be inferred from queries that SELECT EXISTS at the top level of the statement.

The reason I am doing is because I want the generated method to have a (bool, error) return type, so I can easily check if the record exists.

Sample query:

-- name: UserExists :one
SELECT
    EXISTS (
        SELECT
            1
        FROM
            users
        where
            username = $1
    );

Generated code by sqlc:

const userExists = `-- name: UserExists :one
SELECT
    EXISTS (
        SELECT
            1
        FROM
            users
        where
            username = $1
    )
`

func (q *Queries) UserExists(ctx context.Context, username string) (interface{}, error) {
	row := q.db.QueryRowContext(ctx, userExists, username)
	var column_1 interface{}
	err := row.Scan(&column_1)
	return column_1, err
}

Thanks for your work on sqlc, it's an amazing project!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions