Skip to main content

Upsert data: upsert()

Performs an UPSERT into the table.

final res = await supabase
.from('messages')
.upsert({ 'id': 3, 'message': 'foo', 'username': 'supabot' })
.execute();

Notes#

  • Primary keys should to 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.

Examples#

Upsert your data#

final res = await supabase
.from('messages')
.upsert({ 'id': 3, 'message': 'foo', 'username': 'supabot' })
.execute();

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.

final res = await supabase
.from('users')
.upsert({ 'username': 'supabot' }, { 'onConflict': 'username' })
.execute();

Return the exact number of rows#

Allowed values for count option are exact, planned and estimated.

final res = await supabase
.from('users')
.upsert({
'id': 3,
'message': 'foo',
'username': 'supabot'
})
.execute(count: CountOption.exact);