Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres append multiple elements to an array

I have the following table in postgres with the below schema

Relations 

| id | tags |

I need to update the field tags to append unique multiple elements to it I can very well achieve it with like the below

for (String tagId : tagIds ) {

    // Execute the below query in PostGRES
    UPDATE Relations SET tags = array_append (array_remove (tags, '" + tagId + "'), '" + tagId + "') WHERE id = '" + id + "'";

}

But I want to append an array of elements to it in a single go without the for loop. Can someone let me know the query for that ?

like image 952
Aarish Ramesh Avatar asked Jun 29 '26 22:06

Aarish Ramesh


1 Answers

There is no simple solution.

Maybe that is an indication that you are abusing arrays and should store the tags in a table instead.

like image 85
Laurenz Albe Avatar answered Jul 01 '26 12:07

Laurenz Albe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!