Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to encode []model.Field into text format for unknown type (OID 16385): cannot find encode plan #3568

Open
veqryn opened this issue Sep 1, 2024 · 0 comments
Labels

Comments

@veqryn
Copy link

veqryn commented Sep 1, 2024

Version

1.27.0

What happened?

I am trying to create a query that will return a set of results based on a dynamic filter.
I am using the pattern described here: #364 (comment)
However, the query fails due to my ENUM.

I want the query to return any results where the color is any of the filtered colors.
If I change the query to fav_color = @fav_colors::COLORS, then the query works.
But it doesn't seem to like it when using the ANY(@fav_colors::COLORS[]), even though that pattern does work with the regular text/varchar field.

The error message:
&fmt.wrapError{msg:"failed to encode args[5]: unable to encode []model.Colors{\"red\", \"blue\", \"green\"} into text format for unknown type (OID 16385): cannot find encode plan", err:(*fmt.wrapError)(0x14000092620)}

Example calling code:

	accounts, err = dao.SelectAllAccountsByFilter(ctx, model.SelectAllAccountsByFilterParams{
		AnyNames:    true,
		Names:       []string{"Jane", "John"},
		IsActive:    true,
		Active:      true,
		AnyFavColor: true,
		FavColors:   []model.Colors{model.ColorsRed, model.ColorsBlue, model.ColorsGreen},
	})
	if err != nil {
		fmt.Printf("%#+v\n", err)
	}

Relevant log output

No response

Database schema

CREATE TYPE COLORS AS ENUM('red', 'green', 'blue');

CREATE TABLE accounts (
    id          BIGSERIAL PRIMARY KEY,
    name        VARCHAR(50)              NOT NULL,
    email       VARCHAR(50) UNIQUE       NOT NULL,
    active      BOOLEAN                  NOT NULL,
    fav_color   COLORS,
    fav_numbers INTEGER[],
    properties  JSONB,
    created_at  TIMESTAMP WITH TIME ZONE NOT NULL
);

SQL queries

-- name: SelectAllAccountsByFilter :many
SELECT *
FROM accounts
WHERE (CASE WHEN @any_names::bool THEN name = ANY(@names::text[]) ELSE TRUE END)
  AND (CASE WHEN @is_active::bool THEN active = @active ELSE TRUE END)
  AND (CASE WHEN @any_fav_color::bool THEN fav_color = ANY(@fav_colors::COLORS[]) ELSE TRUE END)
;

Configuration

version: "2"
sql:
  - engine: "postgresql"
    queries: "query.sql"
    schema: "../data/01_schema.sql"
    database:
      uri: "postgresql://postgres:password@localhost:5432/learning"
    rules:
      - sqlc/db-prepare
    gen:
      go:
        package: "model"
        out: "internal/model"
        sql_package: "pgx/v5"
        emit_json_tags: true

Playground URL

https://play.sqlc.dev/p/8c40159dc90a74c0fcdb9b84a461e2d4e3608c7a6204f53c878303aa4f09ac0a

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@veqryn veqryn added the bug Something isn't working label Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant