Hi all 👋 I'm trying to update the related values...
# sql
c
Hi all 👋 I'm trying to update the related values on a table. table TH has a link with table TR and I would hope to be able to modify the associations with the following query:
Copy code
const { data, error } = await supabaseClient
      .from('treasure_hunt')
      .update({
        trigger: triggerIds,
      })
      .eq('uuid', id);
but it doesnt work as expected. Instead I am just "brute forcing it" by deleting existing links and recreating the new links
Copy code
await supabaseClient
      .from<definitions['trigger_treasure_hunt_link']>(
        'trigger_treasure_hunt_link'
      )
      .delete()
      .match({ treasure_hunt_uuid: id });

    const { data, error } = await supabaseClient
      .from('trigger_treasure_hunt_link')
      .insert(
        triggerIds.map((trigger) => ({
          treasure_hunt_uuid: id,
          trigger_uuid: trigger,
        }))
      );
Is there a smart way to do this like query 1? Thanks
j
there's a postgres built-in option to update a foreign key like what you need here: https://www.postgresql.org/docs/current/ddl-constraints.html search for 'ON UPDATE' and 'CASCADE' - you'll have to use them when defining your foreign key on the TR table. once it's set up, updating the TH value will 'cascade' the update to TR