title: How do I insert multiple rows with a foreign key using a CTE in Postgres?
body: <p>I want to do a bulk insert transaction but I'm not too sure how to do this using CTEs or a more efficient method.  This is what I have so far:</p>
  
  <pre><code>with bulky as (
      insert into products (title, description, price) 
                    values ('Dope product 1', 'Buy diz', 9.99), 
                           ('Dope product 2', 'Buy diz', 8.99), 
                           ('Dope product 2', 'Buy diz', 7.99) 
                    returning id
  ) 
  insert into product_metadata (product_id, sales_volume, date) 
                         values (???, 80, '2017-03-21'), 
                                (???, 50, '2017-03-21'), 
                                (???, 70, '2017-03-21');
  </code></pre>
  
  <p>The problem with my CTE is I don't know how to get the individual ids from the first insert statement to be inserted to their corresponding records for the second statement which has the 'product_id' foreign key.</p>
  
  <p>How would I construct the statement to make it work?  I'm open to alternative solutions that offer a more efficient method to achieve the same result.</p>