I'm making an UPDATE that should update as many rows as the SELECT query returns.
UPDATE items
SET quantity = items.quantity - (subquery.order_quantity * subquery.join_quantity)
FROM (
SELECT x.id_recipe as id_recipe,
x.quantity as order_quantity,
join_recipes_items.quantity as join_quantity,
join_recipes_items.id_item as id_item
FROM json_to_recordset(${JSON.stringify(recipes_fixed)}::json) as x("id_recipe" int, "quantity" decimal(30,10))
JOIN join_recipes_items USING(id_recipe)
) as subquery
WHERE items.id_item = subquery.id_item;
The last UPDATE should update all rows that achieve the WHERE condition with the values given by the SELECT but it only does some updates.
If you take the SELECT statement apart to see its result:
SELECT x.id_recipe as id_recipe,
x.quantity as order_quantity,
join_recipes_items.quantity as join_quantity,
join_recipes_items.id_item as id_item
FROM json_to_recordset(${JSON.stringify('[ { "id_recipe": 2, "quantity": 3 }, { "id_recipe": 3, "quantity": 2 } ]')}::json) as x("id_recipe" int, "quantity" decimal(30,10))
JOIN join_recipes_items USING(id_recipe);
Using '[ { "id_recipe": 2, "quantity": 3 }, { "id_recipe": 3, "quantity": 2 } ]'
as an example input:
That result is correct but the UPDATE is only using the rows 1,2,5 to update items when it should use all the rows.
What could be happening?