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:
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');
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.
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.

JOINon the title (i.title = v.title) the problem is you cant have two of the same items on the first insert. I did a solution for solve that but is really messy. My suggestion insert product and metadata one by one.