I am working on an e-shop with three tables: products
, atributes
and product_atributes
. I have already filled tables products
and atributes
:
insert into atribute(name)
select random_atribute()
from generate_series(1,10000) as seq(i);
insert into product(price)
select floor(1 + random() * 100)::int
from generate_series(1,20000) as seq(i);
I want to generate random data for the table product_atributes
: There can be 1 attribute for 1 product but also all attributes for just 1 product. The result should look like:
product_atributes
id product_id atribute_id
1 5 12
2 5 76
3 5 10
4 5 7
5 1 45
6 1 109
...
Table product_atributes
is created like this:
create table product_atributes(
id serial primary key,
atribute_id integer references atribute(id),
product_id integer references product(id)
);
I tried sth. like this, but it's not working:
with data as (
select s.i,
random_atribute_id() as atribute_id,
s.id as product_id
from (generate_series(1, 1000) as seq(i)
cross join lateral (select seq.i, * from product order by random() limit 1) as s)
)
insert into product_atributes(atribute_id, product_id)
select atribute_id, product_id from data;
How can I do this?