Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update multiple rows in a single query in a Supabase Database (postgres)

I looking to update multiple rows via a single query in Supabase. I'm struggling to find an example of how to do it online.

For this example here is how the data exists the database's 'food' table:

id title qty
1 Apple 10
2 Banana 8
3 Mango 4

What i would like to do is update the 3 qty fields in the single query (this code doesn't work but should give you an idea of what i am after).

const { data: item_data, error: item_error } = await supabase
   .from('food')
   .update({ qty: 11, }).eq('id', '1')
   .update({ qty: 9, }).eq('id', '2')
   .update({ qty: 6, }).eq('id', '3')
like image 206
Jake Anderson Avatar asked Jul 18 '26 16:07

Jake Anderson


1 Answers

You can do it with an upsert():

const { data, error } = await supabase
  .from('food')
  .upsert([{id: 1, qty: 11}, {id: 2, qty: 9}, {id: 3, qty: 6}])

Additionally you can also do it with a SQL function as mentioned here.

like image 160
Steve Chavez Avatar answered Jul 25 '26 05:07

Steve Chavez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!