sequelize query to update several rows with information from a related table

Viewed 10

im currently developing a shopping cart with the following business logic and need to update the items simultaneously with the latest prices at the time of purchase with the simplest sequelize query posible

Fist the user creates a new item by adding a product to it's cart, with it's asociated productId, userId and quantity

id salePrice quantity subtotal status productId userId orderId
1 null 5 null 0 23 8 null
2 null 1 null 0 76 8 null
3 null 2 null 0 193 8 null

then when the user makes the purchase the item is updated to reflect the current price of the product at the time of purchase with any applying discounts or price changes and the subtotal amount taken from salePrice * quantity

id salePrice quantity subtotal status productId userId orderId
1 68 5 340 1 23 8 35
2 200 1 200 1 76 8 35
3 500 2 1000 1 193 8 35

so the question is how to update the salePrice and subtotal with information from the related product to multiple items simultaneusly using sequelize

1 Answers

Use MySQL's multi-table update syntax:

update cart, product set
salePrice = price,
subtotal = price * quantity
where product.id = productId
and orderId = 35
Related