Skip to content

sqlc.embed isn't wrapping text[] fields in a pq.Array #2458

Closed
@will-wow

Description

@will-wow

Version

1.18.0

What happened?

We're using (and loving!) the new sqlc.embed function, but ran into what seems to be a bug, where using embed causes sqlc not to wrap text[] fields in a pq.Array function, which causes a unsupported Scan, storing driver.Value type <nil> into type *[]string at runtime. Replacing the sqlc.embed(records) call with a records.* fixes the array issue, but of course means maintaining a conversion function into a struct.

This happened on a query doing some joins, but in the reproduction also happens on a single-table query.

Minimal Reproduction

In the sqlc Playground, you can see that:

  • the text_list and int_list fields are of type text[] and int[]
  • first query uses a records.* for SELECT, and the second query uses sqlc.embed(records)
  • These should have essentially the same output, but in the generated query.sql.go file, the first query wraps the lists in a pq.Array, while the second does not.
  • This leads to an unsupported Scan, storing driver.Value type <nil> into type *[]string error at runtime.

Relevant log output

unsupported Scan, storing driver.Value type <nil> into type *[]string

Database schema

CREATE TABLE records (
  id   BIGSERIAL PRIMARY KEY,
  text_list text[],
  int_list int[]
);

SQL queries

-- name: GetRecords :many
SELECT *
FROM records;

-- name: GetRecordsWithEmbed :many
SELECT 
    sqlc.embed(records)
FROM
    records;

Configuration

{
  "version": "1",
  "packages": [
    {
      "path": "db",
      "engine": "postgresql",
      "schema": "query.sql",
      "queries": "query.sql"
    }
  ]
}

Playground URL

https://play.sqlc.dev/p/bd6670f67d7952178f3aa96d510cc9f84787bbb079fe791eeaca80fa33300606

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageNew issues that hasn't been reviewed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions