Insert data in several related tables at once
I have three tables:
workouts:
id | userId(foreign key) | date | exercises_id
exercises:
id | workout_id (foreign key) | sets_id(foreign key)
sets:
id | exercise_id(foreign key) | reps | weight
The ui of the app: user creates a workout, adds exercises, sets and then click the save button.
Question: how can I store all the data at once into sql? When I run the query
I get the error that there is no
Is it possible to run a query the way that it'll insert the data in all 3 tables at once and generate all the required foreign/primary keys?
The only solution I was able to come up with is to use JSONB for storing exercises with sets instead of creating several tables for them but I'd prefer to keep them in separate tables.
workouts:
id | userId(foreign key) | date | exercises_id
exercises:
id | workout_id (foreign key) | sets_id(foreign key)
sets:
id | exercise_id(foreign key) | reps | weight
The ui of the app: user creates a workout, adds exercises, sets and then click the save button.
Question: how can I store all the data at once into sql? When I run the query
I get the error that there is no
exercises_id and it make sense since there wasn't created yet a table for exercises. Is it possible to run a query the way that it'll insert the data in all 3 tables at once and generate all the required foreign/primary keys?
The only solution I was able to come up with is to use JSONB for storing exercises with sets instead of creating several tables for them but I'd prefer to keep them in separate tables.