Chain Supabase Insert(s)
Wondering if I can chain a Supabase insert, for example:
Insert into table x
Select newly inserted row
Insert into table y with id of newly inserted row
And if so, how would a JS function look like?
5 Replies
I thought since Supabase is a promise I could chain a .finally to it but it appears that's not working
Think I figured it out! 😄
This works
this was my first attempt and didn't work
You can also use an rpc call so they are also run as a transaction so if either insert fails, they both fail. And you only have one API call overhead.
You would use insert returning in sql to get the id for the first inserted row, to use in the next insert call.
Otherwise a postgres google would find you the two insert code in SQL.
Uff, I've not worked with those RPCs yet but will take that into consideration
You can easily do with two calls in the client, you just need to handle error cases for each insert as appropriate. In particular if the 2nd insert fails do you need to delete the first insert or not.
Yeah makes sense, thanks for helping out Gary!