Skip to content

Query with COUNT() subselect generates bool field instead of int #1042

@ulexxander

Description

@ulexxander

Field gets invalid type which in my case causes runtime error when scanning into struct:
sql: Scan error on column index 6, name "orders_count": sql/driver: couldn't convert 14 into type bool

I also managed to reproduce it in the sqlc playground:

query.sql

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

CREATE TABLE books (
  id BIGSERIAL PRIMARY KEY,
  author_id BIGSERIAL NOT NULL
  	REFERENCES authors(id),
  title text NOT NULL
);

-- name: GetAuthorsWithBooksCount :many
SELECT *, (
  SELECT COUNT(id) FROM books
  WHERE books.author_id = id
) AS books_count
FROM authors;

Code generated:
image

Noticed interesting thing: if i remove AS statement (column alias) it will give this field name Exists

type GetAuthorsWithBooksCountRow struct {
	ID     int64
	Name   string
	Bio    sql.NullString
	Exists bool
}

I think it may help fix this bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions