porsager / porsager/postgres

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

Open
#811 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the UPDATE using the shown TypeScript template and the sql(keywords_to_insert) interpolation, comparing the generated column names and parameter types with the supplied objects. Trace how this interpolation handles an array of objects; done means determining whether the result is expected syntax or a postgres.js inference bug.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, typescript
Domain
backend, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.