0

My question is the same as UPDATE multiple rows from multiple params in nodejs/pg however I face the issue the question author pointed out in the comments.

Basically, I want to perform a multi-row update and have such an array of objects like

const arr = [
  { image_id: 1, image_url: "xyz.com/image" }
]

Now I want to use that array to generate the SQL statement but just like the author in the link above, I don't know how to make an array of objects into an array of tuples like SQL expects.

1 Answer 1

0

The following worked but I had to change my data structure to:

{
    "images": ["xyz.com/image"],
    "ids": [2]
}
UPDATE collection_images as ci 
SET image_url = new.image_url 
FROM (SELECT UNNEST($1::VARCHAR[]) as image_url, UNNEST($2::INT[]) as image_id) as new
WHERE ci.id = new.image_id
RETURNING *;

But I'm still wondering whether there's a way without changing the orignal data structure

Sign up to request clarification or add additional context in comments.

1 Comment

note this wouldn't return all the updated rows. the RETURNING * at the end would only return the last modified row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.