Skip to main content
Version: v1-dev

Upsert data: upsert()

Performs an UPSERT into the table.

final data = await supabase
.from('messages')
.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

final data = await supabase
.from('messages')
.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.

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

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'},
options: const FetchOptions(count: CountOption.exact),
);

final data = res.data;
final count = res.count;