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

values helper not working as expected #1042

Open
Sebbones opened this issue Mar 5, 2025 · 0 comments
Open

values helper not working as expected #1042

Sebbones opened this issue Mar 5, 2025 · 0 comments

Comments

@Sebbones
Copy link

Sebbones commented Mar 5, 2025

I was trying to get something like the following MERGE statement running:

const rows = [
  { id: 1, name: "Ana" },
  { id: 2, name: "Peter" },
]

// use VALUES in fragment to trigger values helper
const valuesFragment = sql`VALUES ${sql(rows, 'id', 'name')}`

sql`
MERGE INTO "table"
USING ( ${valuesFragment} ) AS "values" ( "id", "name" )
...
`

But this ended up throwing the UNDEFINED_VALUE, 'Undefined values are not allowed error.

After some digging i found the reason to be that the first argument of types.js:valuesBuilder() is an array of array:

[
  [
    { id: 1, name: "Ana" },
    { id: 2, name: "Peter" },
  ]
]

so accessing the columns value for 'id' and 'name' failed because the row is an array and not the row object itself.

I think the issue for this in types.js:131 where multi is calculated from first[0] instead of just first, like in the insert helper.
After switching to just first it worked for my VALUES statement but i am not sure if i broke somehing else.

Is my assumption correct or am i using the values helper incorrectly?

Happy to submit a fix if this is actually a bug :)

Edit

Yes, i broke something. Using arrays of primitive values no longer works (like in: id IN ${psql([1, 2])}. Ended up changing the builder for in to wrap it's first parameter sent to the values builder always as a nested array.

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

No branches or pull requests

1 participant