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

Seems that unnest does not support arrays #531

Open
oxilor opened this issue Jan 26, 2024 · 1 comment
Open

Seems that unnest does not support arrays #531

oxilor opened this issue Jan 26, 2024 · 1 comment

Comments

@oxilor
Copy link

oxilor commented Jan 26, 2024

Let's say we have the following table:

CREATE TABLE roles (
  name varchar(100) NOT NULL,
  file_types text[] DEFAULT ARRAY[]::text[] NOT NULL
);

and we want to use unnest to populate the table with data (only one row just for simplicity):

const data = await pool.any(sql.unsafe`
    INSERT INTO roles (name, file_types)
    SELECT *
    FROM ${sql.unnest(
      [['name', ['IMAGE']]],
      ['text', sql.fragment`text[]`] // 'text[]' also does not work because it generates $2::"text[]"[]
    )}
    RETURNING *
`);

In this case, the following error is occured:

error: column "file_types" is of type text[] but expression is of type text

Generated sql:

INSERT INTO roles (name, file_types)
SELECT *
FROM unnest($1::"text"[], $2::text[][])
RETURNING *

Generated values:

[[ 'name' ], [ [ 'IMAGE' ] ] ]

P.S. Inserting rows using INSERT INTO roles VALUES (${'name'}, ${sql.array(['IMAGE'], 'text')}) works fine.

@oxilor oxilor added the bug label Jan 26, 2024
@gajus gajus added enhancement and removed bug labels Jan 26, 2024
@gajus
Copy link
Owner

gajus commented Mar 12, 2024

Consider this pattern instead #481 (comment)

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

No branches or pull requests

2 participants