I have a table in Postgres that looks something like:
TABLE Product (
id serial,
name varchar(512),
price numeric,
...
)
I am trying to create a procedure that would take an array of json objects and insert each element as a row into the table.
I am doing something like this:
CREATE OR REPLACE PROCEDURE insert_into_product(entries json[])
LANGUAGE plpgsql AS $$
DECLARE _elem json;
BEGIN
FOREACH _elem IN ARRAY entries
LOOP
INSERT INTO product
SELECT id, name, price
FROM json_populate_record (NULL::product,
format('{
"id": "%d",
"name": "%s",
"price": "%f"
}', _elem->id, _elem->name, _elem->price)::json
);
END LOOP;
END;
$$
And I'm getting an error "column "id" does not exist"
Is the problem here that id is serial, and in my json object it's an int? What's the right way to insert row from a json object here?
json_populate_recordneeds to be called with the_elemas the parameter.format()returns a string value, not a JSON