Upsert using Postgresql function
# help
v
Upsert using Postgresql function
How can I write a function that take an array of records, then upsert them to the database. I know there is
upsert()
method, but I have to use 5 queries to upsert to 5 tables, so I want to create a function that do all of that in just one query. How can I do that? Thanks
g
You mention 5 tables, I assume you have 5 records that each go to a different table in the database (versus 5 records to same table). If so, your only choice is 5 calls or to write an RPC function that does 5 sql updates in it and pass it all your records. RPC, seems pretty messy since api calls are "free" on supabase and you are passing the same amount of data. (yes there is overhead). If all 5 must be executed together as a batch or fail together, then RPC is the only way. Remember RPC function are just postgresql functions so you can get info on how to write them searching with postgresql functions.
v
Umm each record has foreign columns so i have to insert those columns into different tables
g
OK. So without more info, sounds like you need to either do all 5 api calls or write a pretty complicated RPC function that takes 5 records in and does all the SQL you want. Sorry it is hard to know what you want to do with no real description of operations…
v
well i think 5 api calls is fine, thank you anyway
4 Views