Ludvig
05/09/2022, 12:16 PMsql
CREATE FUNCTION test_function(products_object_array_input some_data_type) -- what should the data type be? array? jsonb[]?
An array of objects that I would pass into the function could look like this:
json
[
{ "productID": 3, "quantity": 5 },
{ "productID": 1, "quantity": 2 }
]
First question: What should I set the products_object_array_input data type as? array? jsonb[]?
Second question: I then want to loop over the array to insert the values of the object array. Again, using plpgsql. How?
sql
-- I want to loop over cart_array_input and do this for every object in the array. Sorry for pseudo-code. i = iterator
insert into order_item(product_id, quantity)
values(products_object_array_input[i].productID, products_object_array_input[i].quantity);
Scott P
05/09/2022, 7:56 PMJSONB[]
would be different, and would look like this:
{
[
{ "productID": 3, "quantity": 5 },
{ "productID": 1, "quantity": 2 }
]
}
As you can see, not valid JSON but a valid PG array of JSON / JSONB (since {
and }
are the array markers in PG unless you tell PG that it's all JSON.
JSONB would be my recommendation. For all intents and purposes, it appears as normal JSON to us, but it's possible for PG to optimise how it's stored, indexed and accessed, and some JSON operators only work on JSONB specifically.
For working with the actual contents of the JSON, you would need to use the JSON-specific operators: https://www.postgresql.org/docs/9.3/functions-json.html
dot.notation isn't used for traversing through data in PGLudvig
05/09/2022, 11:17 PMLudvig
05/10/2022, 10:45 AMsql
create table incoming_order(...) -- id, and other columns
create table product(...) -- id, and other columns
create table order_item(
id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
product_id int not null,
CONSTRAINT fk_product FOREIGN KEY(product_id) REFERENCES product(id),
quantity int not null,
incoming_order_id int not null,
CONSTRAINT fk_order FOREIGN KEY(incoming_order_id) REFERENCES incoming_order(id)
); -- An incoming_order can have many order_items.
create function new_incoming_order(cart_input json)
-- cart_input will be an array with products (objects). Could look something like this:
-- [{product_id: 2, quantity: 3}, {product_id: 1, quantity: 4}]
returns setof incoming_order
as $$
declare
var_incoming_order_id int;
begin
insert into incoming_order(some_column)
values(some_value)
returning id into var_incoming_order_id;
create type some_type as (product_id int, quantity int);
insert into order_item(
select * from json_populate_recordset(null::some_type, cart_input)
);
-- What is left and that I need help with is, I need to insert var_incoming_order_id into order_item.incoming_order_id TOGETHER with every new row created from json_populate_recordset. Maybe I should use something other than json_populate_recordset? But what then? And how would it look like?
return query select * from incoming_order where incoming_order.id = var_incoming_order_id;
end;
$$ language plpgsql
Ludvig
05/10/2022, 2:01 PMjson
[
{product_id: 1, quantity: 4},
{product_id: 2, quantity: 3}
]
->
json
[
{product_id: 1, quantity: 4, incoming_order_id: 10},
{product_id: 2, quantity: 3, incoming_order_id: 10}
]
Scott P
05/10/2022, 3:01 PMjsonb_set
function. There's a good overview with some examples at https://aaronbos.dev/posts/update-json-postgresql
e.g.:
sql
UPDATE public.person
SET personb = jsonb_set(personb, '{interests, 2}', '"Teaching"', true)
WHERE personb ->> 'last_name' = 'Ellis';
Ludvig
05/10/2022, 4:02 PMsql
UPDATE cart_input
SET cart_input = jsonb_set(cart_input, '{incoming_order_id, 2}', var_incoming_order_id, true);
insert into order_item(
select * from jsonb_populate_recordset(null::order_item, cart_input)
);
but get this error:
{
message: 'relation "cart_input" does not exist',
code: '42P01',
details: null,
hint: null
}
Ludvig
05/10/2022, 4:03 PMUPDATE cart_input
line, I get syntax error at or near "("