porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.04k stars 257 forks source link

query is not inferred correctly when doing multiple update, and a value is an array. #811

Open raprocks opened 4 months ago

raprocks commented 4 months ago

please let me know if i am doing something wrong lol. i am trying to update multiple records in a table that has one of the columns as an varchar[] and other columns are of type int and text.

example input:

[{"project_id":83,"keyword":["adasda"],"type":"APARTMENT_CONTAINS"},{"project_id":83,"keyword":[],"type":"COUNTERPARTY_CONTAINS"},83]

typescript code, updateRes.id is a number equivalent to project_id as seen in params below in debug info:

const _keywords_insert_res = await sql<{ id: number }[]>`
UPDATE public.keywords
SET
  ${sql(keywords_to_insert)}
WHERE project_id = ${updateRes.id}
`;

generated query:

UPDATE public.keywords
   SET
     "0"=$1,"1"=$2
   WHERE project_id = $3

other debug info:

 Params: [{"project_id":83,"keyword":["adasda"],"type":"APARTMENT_CONTAINS"},{"project_id":83,"keyword":[],"type":"COUNTERPARTY_CONTAINS"},83]
 Param Types: [0,0,0]

Why is the generated query taking column names as 0 and 1 when i have passed in the array of object, i.e, instead of project_id or keyword or type. i might be surely doing something wrong but would be great to know if this is not an issue and it is just me being dumb :P is this just a syntax error on my end for postgres.js or postgresdb?

raprocks commented 4 months ago

i fixed it for now by using insert ... ON CONFLICT DO UPDATE SET keywords = EXCLUDED.keywords

still would like to know what is wrong,.