This is so simple query. I need to update Bookings tables’, LinkedBookings column (int array data type) to it’s own Id. Below query didn’t work for me.
update public."Booking" b set "LinkedBookings" = {"Id"}
Above is giving below error,
SQL Error [42601]: ERROR: syntax error at or near "{"
I searching thorough the internet and postgres documentation and couldn’t find any proper simple query. I can understand there is a way to do with writing a Pg Function. Rather than doing such a big thing, isn’t there any simple query for that?
2
Answers
Assuming
LinkedBookings
is an array column and assuming that you want to set it to an array containing only theId
of each row, the correctSQL
should be >>Here’s the the doc you were looking for.
The syntax you used is for literals – you got the error because you’re missing the single quotes around the brackets, and the entire thing should be a
text
before it can become anint[]
array. Demo at db<>fiddle:The alternative syntax is wrapping the value in
ARRAY[]
, already pointed out. You can add the explicit cast to be sure:If you’re just making
"Id"
available in an alternative form to save array-wrapping elsewhere, you can consider setting up"LinkedBookings"
as a generated column – that way you don’t need to keep refreshing the table with periodicupdate
.If you’re running this update to pre-populate the column and you plan to modify it later, don’t make it
generated
as those cannot be modified directly. They can only be re-generated by modifying the values they depend on.