JavaScript: Upsert data

Perform an UPSERT on the table or view. Depending on the column(s) passed to onConflict, .upsert() allows you to perform the equivalent of .insert() if a row with the corresponding onConflict columns doesn't exist, or if it does exist, perform an alternative action depending on ignoreDuplicates.

Parameters

Examples

Upsert your data

const { data, error } = await supabase
  .from('instruments')
  .upsert({ id: 1, name: 'piano' })
  .select()

Bulk Upsert your data

const { data, error } = await supabase
  .from('instruments')
  .upsert([
    { id: 1, name: 'piano' },
    { id: 2, name: 'harp' },
  ])
  .select()

Upserting into tables with constraints

const { data, error } = await supabase
  .from('users')
  .upsert({ id: 42, handle: 'saoirse', display_name: 'Saoirse' }, { onConflict: 'handle' })
  .select()