with new_order as( insert into orders(email, total) values ('email@example.com',100.00) returning * ), new_items as ( insert into order_items(order_id, sku, price) select new_order.id, 'imposter-single',30.00 from new_order returning * ), new_downloads as ( insert into downloads(order_id, order_item_id) select new_order.id, new_items.id from new_order, new_items returning * ) select * from new_downloads;
I tack on a
returning *from my insert statement for
order_itemsand then I can use that to generate the downloads in a third query, this time using a
selectfor the insert.