© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•4y ago•
8 replies
Dmitrii

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
const { data, error } = await supabase
  .from('exercises')
  .insert(exercisePayload)
const { data, error } = await supabase
  .from('exercises')
  .insert(exercisePayload)


I get the error that there is no
exercises_id
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.
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Insert 3 rows at once
SupabaseSSupabase / help-and-questions
4y ago
Insert method, can't insert data
SupabaseSSupabase / help-and-questions
4y ago
Tables In Tables?
SupabaseSSupabase / help-and-questions
4y ago
How to insert to multiple tables in one request?
SupabaseSSupabase / help-and-questions
4y ago