Skip to main content
Version: v1

Upsert data: upsert()

Performs an UPSERT into the table.

const { data, error } = await supabase
.from('messages')
.upsert({ id: 3, message: 'foo', username: 'supabot' })

Parameters

  • valuesrequiredPartial | array

    The values to insert.

      Properties
    • objectrequiredobject

      No description provided.

    • Partialrequiredobject

      No description provided.

  • __namedParametersrequiredobject

    No description provided.

      Properties
    • countoptionalliteral | exact | planned | estimated

      No description provided.

        Properties
      • estimatedrequiredliteral

        No description provided.

      • plannedrequiredliteral

        No description provided.

      • exactrequiredliteral

        No description provided.

      • nullrequiredliteral

        No description provided.

    • ignoreDuplicatesoptionalboolean

      No description provided.

    • onConflictoptionalstring

      No description provided.

    • returningoptionalminimal | representation

      No description provided.

        Properties
      • representationrequiredliteral

        No description provided.

      • minimalrequiredliteral

        No description provided.

Notes

  • Primary keys should be included in the data payload in order for an update to work correctly.
  • Primary keys must be natural, not surrogate. There are however, workarounds for surrogate primary keys.
  • If you need to insert new data and update existing data at the same time, use Postgres triggers.

Examples

Upsert your data

const { data, error } = await supabase
.from('messages')
.upsert({ id: 3, message: 'foo', username: 'supabot' })

Bulk Upsert your data

const { data, error } = await supabase
.from('messages')
.upsert([
{ id: 3, message: 'foo', username: 'supabot' },
{ id: 4, message: 'bar', username: 'supabot' }
])

Upserting into tables with constraints

Running the following will cause supabase to upsert data into the users table. If the username 'supabot' already exists, the onConflict argument tells supabase to overwrite that row based on the column passed into onConflict.

const { data, error } = await supabase
.from('users')
.upsert({ username: 'supabot' }, { onConflict: 'username' })

Return the exact number of rows

const { data, error, count } = await supabase
.from('users')
.upsert({
id: 3, message: 'foo',
username: 'supabot'
}, {
count: 'exact'
})