question 292 Packaging & Databases · hard
Half-saved orders
Warehouse staff found orders in the database with only some of their lines, and stock taken for orders that the customer was told had failed. place_order raises ValueError when a line asks for more units than are in stock, but by then part of the order is already saved. Fix it so an order is saved completely or not at all, and so a failed order leaves nothing behind that a later order could commit by accident.
Function
place_order(conn, order_id, lines)Tables
stock (sku, units), orders (order_id), order_lines (order_id, sku, quantity)Mustsave the order row, every line and every stock change together, and commit them
On failureraise
ValueError (as now), with the database exactly as before the call