I have user_product_mapping
storing product per user:
DROP TABLE IF EXISTS user_product_mapping;
CREATE TABLE user_product_mapping
(
id SERIAL NOT NULL PRIMARY KEY,
user_id INT,
product_info json NOT NULL,
CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(user_id)
);
Sample values:
INSERT INTO user_product_mapping (user_id, product_info)
VALUES
(1, '{"product": "laptop,mobile"}'),
(2, '{"product": "charger"}'),
(3, '{"product": "mobile,mouse,charger"}')
;
Now I want to add a new product as 'laptop' to existing user_id 2.
Expected result:
user_id | product_info
---------------------------------------
2 | {"product": "charger,laptop"}
I have tried to append but face an issue:
UPDATE user_product_mapping
SET product_info = product_info || '{"laptop"}'
WHERE user_id = 2;
Error:
ERROR: column "product_info" is of type json but expression is of type text
LINE 2: SET product_info = product_info || '{"123e4567-e89b-12d3-a45...
^
HINT: You will need to rewrite or cast the expression.
Can you please suggest the way forward.