-2

Very innocent question

If we have an array of items in a table cell, is there any way to append data to it without having the need to fetch the data of the whole row to get its content, in order to finally push the updated data to the cell?

I have a column named content, which is an array of file identifiers, and I want to append some new data to it.

Is there any clean way to do this action? Or we need to perform a select() before an update()?

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68
chipseater_
  • 3
  • 1
  • 3

1 Answers1

0

It is generally better to create a separate table to store 1 to many relationships than to store them in an array, but with that being said, you could achieve appending items to array by using rpc() and calling your Postgres function.

You could create a function like the following to append array values:

create or replace function append_array(new_element text, id uuid)
returns void
as
$func$
    update array_table
    set array_element = array_append(array_element, $1)
    where id = $2
$func$
language sql;

You can read more about this here

dshukertjr
  • 15,244
  • 11
  • 57
  • 94