0

I am trying to add a new integer value to my existing integer array in postgres table. I am using array_append method here but getting "Cannot cast type jsonb to integer[]" error. My update query is as follows.

Note- I am just using it for learning purpose so I am intentionally saving phonenumber as integer.

Click here for table structure and error screenshot

update querytesting set jsondoc=jsondoc||jsonb_build_object(jsondoc->'PhoneNumber',array_append(((jsondoc->'PhoneNumber')::int[]),'6789')) where id=1;

1
  • Sample data is better presented as formatted text. See here for some tips on how to create nice looking tables. Commented Sep 30, 2021 at 6:50

1 Answer 1

1

You have to extract the array, append the new number, create a new JSON with the new array and store that. A tedious and inefficient procedure that would be unnecessary if you had stored your data in a normalized data model instead of a JSON.

UPDATE querytesting
SET jsondoc = jsonb_set(
                 jsondoc,
                 '{PhoneNumber}',
                 jsondoc -> 'PhoneNumber' || JSONB '[6789]'
              );
WHERE id=1;
Sign up to request clarification or add additional context in comments.

Comments

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.