Skip to content

Composite types are not understood #2177

Closed as not planned
Closed as not planned

Description

@tobbstr

Version

1.17.2

What happened?

Given

Migration file

create type myarg as (
    id integer,
    name text
);

When

Query file

-- name: BasicQuery :one
select $1::myarg;

Then

const basicQuery = `-- name: BasicQuery :one
select $1::myarg
`

func (q *Queries) BasicQuery(ctx context.Context, dollar_1 string) (string, error) {
	row := q.db.QueryRow(ctx, basicQuery, dollar_1)
	var column_1 string
	err := row.Scan(&column_1)
	return column_1, err
}

Note that the second input parameter (dollar_1 string) is of type string, which is not what I expected. The same goes for the output parameter which I expected to a struct but is instead a string.

When

Query file

-- name: LessBasicQuery :many
select id, text from unnest(@args::myarg[]);

Then

When you generate the Go code the following error shows up in the terminal.

queries/queries.sql:28:8: column "id" does not exist
sql/generate.go:3: running "sqlc": exit status 1

When

Query file

-- name: LessBasicQuery :many
select * from unnest(@args::myarg[]);

Then

const lessBasicQuery = `-- name: LessBasicQuery :many
select  from unnest($1::myarg[])
`

type LessBasicQueryRow struct {
}

func (q *Queries) LessBasicQuery(ctx context.Context, args []string) ([]*LessBasicQueryRow, error) {
	rows, err := q.db.Query(ctx, lessBasicQuery, args)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []*LessBasicQueryRow
	for rows.Next() {
		var i LessBasicQueryRow
		if err := rows.Scan(); err != nil {
			return nil, err
		}
		items = append(items, &i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

Notice that the input argument (args []string) is a slice of string instead of an expected struct. The output parameter LessBasicQueryRow is struct, but its implementation is empty i.e. no fields have been generated.

Relevant log output

No response

Database schema

create type myarg as (
    id integer,
    name text
);

SQL queries

No response

Configuration

version: '2'
sql:
  - schema: "migrations/"
    queries: "queries/"
    engine: "postgresql"
    gen:
      go:
        package: "store"
        out: "./store"
        sql_package: "pgx/v4"
        emit_result_struct_pointers: true
        emit_params_struct_pointers: true
        emit_interface: false
        output_db_file_name: "db.go"

Playground URL

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

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Activity

  1. added
    bugSomething isn't working
    triageNew issues that hasn't been reviewed
    on Mar 30, 2023
  2. louisrli commented on Apr 6, 2023

    @louisrli

    Seeing the same thing -- tested in a small repo as well with pgx/v4, pgx/v5 and sql, and it all yields the same result

  3. louisrli commented on Apr 6, 2023

    @louisrli

    Or perhaps it's intended, not sure how it worked before:
    #311

    I'll give it a try by using the overrides in the config file

  4. changed the title [-]Composite types are not understood by sqlc[/-] [+]Composite types are not understood[/+] on Jun 7, 2023
  5. kyleconroy commented on Sep 26, 2023

    @kyleconroy
    Collaborator

    Tracking in #2760

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions