Upsert data: upsert()

Performs an UPSERT into the table.

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

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.

Examples#

Upsert your data#

1final data = await supabase
2  .from('messages')
3  .upsert({ 'id': 3, 'message': 'foo', '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.

1final data = await supabase
2  .from('users')
3  .upsert({ 'username': 'supabot' }, { 'onConflict': 'username' });

Return the exact number of rows#

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

1final res = await supabase.from('users').upsert(
2  {'id': 3, 'message': 'foo', 'username': 'supabot'},
3  options: const FetchOptions(count: CountOption.exact),
4);
5
6final data = res.data;
7final count = res.count;