https://supabase.com/ logo
So I want to add for simplicity for example a list...
# help
j
So I want to add for simplicity for example a list of colors for a user in a join table user_colors. If I insert/upsert this part is from front end so js list ["red","green","blue"] or whatever and then I want the user_colors to remove the ones that I dont send and keep/add ones I do so if on front end toggle off blue and maybe add purple so new list is ["red","green","purple"] I was trying
Copy code
supabase.from('user_colors').match({user_id: some_user_id}).delete() and supabase.from('user_colors').insert([{user_id: some_user_id, color_id: "red"},{user_id: some_user_id, color_id: "green"},{user_id: some_user_id, color_id: "purple"}])
however I get error that I cannot do two operations on same row aka deleting red and adding red. perhaps I need to not that red and green are maintained and use .not on the delete for those? In which case how can I chain methods of nots in a loop
Copy code
supabase.from('user_colors').match({user_id: some_user_id}).delete().not('color_id', 'eq', 'red').not('color_id', 'eq', 'green')
I assume is how i do then just upsert. But I have js list so how can i do for color in colors loop to add .nots in chain. And if better way lmk thanks!
n
Hello @jar! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
g
SB client API can only do one operation at a time. You can do a bulk insert or delete more than one row as the only multiple operation. You can chain filters in a single operation but can not chain operations. You may need to use a rpc call to a Postgres function to do what you want, but it is unclear what your structure is for this color info.
j
Gotcha. I think ill just put ones to delete in list and ones to add in list or do rpc. Thanks!
n
Thread was archived by @jar. Anyone can send a message to unarchive it.
j
https://github.com/supabase/supabase/issues/4832 actually now have issue with the bulk delete part knowing set of primary keys that need deleted. They are joint primary keys
g
You would just use a filter like .in with your list of ids, or match with multiple entries. Oh, are you saying you don't know how to identify the rows?
j
Ok i think something with in or maybe both but how would that look like?
Copy code
const { data, error } = await supabase
  .from('user_colors')
  .delete()
  .match({user_id : bill})
  .in('color_id', ['red', 'green'])
This looks possibly good
g
The .in part looks correct.
j
(primary key being (user_id, color_id)
ok yeah lemme try
ug I think delete thing is right but still calling a delete that only affects certain rows and an add that adds new rows gave me
Copy code
error:
code: "21000"
details: null
hint: "Ensure that no rows proposed for insertion within the same command have duplicate constrained values."
message: "ON CONFLICT DO UPDATE command cannot affect row a second time"
Hmm Ill keep trying to figure out. May just do rpc but gonna try this first without
n
Thread was archived by @jar. Anyone can send a message to unarchive it.
2 Views