I want to use these SQL queries to generate test data in PostgreSQL:
INSERT into onboarding_tasks (business_name, updated_at)
SELECT
'Business name ' || id AS business_name,
(NOW() (random() * (NOW() '90 days' - NOW())) '30 days') AS updated_at
FROM generate_series(1,25) as g(id);
Generate 4 items into database table using parent id:
INSERT into onboarding_task_item (created_at, description, task_id)
SELECT
(NOW() (random() * (NOW() '90 days' - NOW())) '30 days') AS created_at,
left (md5(random()::text), 10) AS description,
ot.id,
j.id AS gen_id
FROM onboarding_tasks ot
CROSS JOIN generate_series(1,4) j(id);
When I run the second SQL query I get:
Caused by: org.postgresql.util.PSQLException: ERROR: INSERT has more expressions than target columns
Position: 486
I'm not sure how the table items should be edited in order to use ot.id, j.id AS gen_id
in order to make this correct.
Do you know how I can fix this issue?
CodePudding user response:
Because you use three columns in insert statement INSERT into onboarding_task_item (created_at, description, task_id)
but send four columns in the select statement
SELECT
... AS created_at, -- first column
... AS description, -- second coloumn
ot.id, -- third coloumn
j.id AS gen_id -- fourth column
FROM onboarding_tasks ot
CROSS JOIN generate_series(1,4) j(id);