mirror of
https://github.com/supabase/supabase.git
synced 2026-07-05 07:14:28 +08:00
128 lines
2.2 KiB
Plaintext
128 lines
2.2 KiB
Plaintext
---
|
|
id: upsert
|
|
title: "Upsert data: upsert()"
|
|
slug: upsert
|
|
custom_edit_url: https://github.com/supabase/supabase/edit/master/web/spec/dart.yml
|
|
---
|
|
|
|
import Tabs from '@theme/Tabs';
|
|
import TabsPanel from '@theme/TabsPanel';
|
|
|
|
Performs an UPSERT into the table.
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="dart"
|
|
groupId="reference/dart"
|
|
values={[{ label: 'Dart', value: 'dart' }]}>
|
|
|
|
<TabsPanel id="dart" label="dart">
|
|
|
|
```dart
|
|
final res = await supabase
|
|
.from('messages')
|
|
.upsert({ 'id': 3, 'message': 'foo', 'username': 'supabot' })
|
|
.execute();
|
|
```
|
|
|
|
|
|
</TabsPanel>
|
|
|
|
</Tabs>
|
|
|
|
|
|
|
|
|
|
|
|
## 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](https://github.com/PostgREST/postgrest/issues/1118) for surrogate primary keys.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
### Upsert your data
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="dart"
|
|
groupId="reference/dart"
|
|
values={[{ label: 'Dart', value: 'dart' }]}>
|
|
|
|
<TabsPanel id="dart" label="dart">
|
|
|
|
```dart
|
|
final res = await supabase
|
|
.from('messages')
|
|
.upsert({ 'id': 3, 'message': 'foo', 'username': 'supabot' })
|
|
.execute();
|
|
```
|
|
|
|
|
|
</TabsPanel>
|
|
|
|
</Tabs>
|
|
|
|
### 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`.
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="dart"
|
|
groupId="reference/dart"
|
|
values={[{ label: 'Dart', value: 'dart' }]}>
|
|
|
|
<TabsPanel id="dart" label="dart">
|
|
|
|
```dart
|
|
final res = await supabase
|
|
.from('users')
|
|
.upsert({ 'username': 'supabot' }, { 'onConflict': 'username' })
|
|
.execute();
|
|
```
|
|
|
|
|
|
</TabsPanel>
|
|
|
|
</Tabs>
|
|
|
|
### Return the exact number of rows
|
|
|
|
Allowed values for count option are `exact`, `planned` and `estimated`.
|
|
|
|
|
|
<Tabs
|
|
defaultActiveId="dart"
|
|
groupId="reference/dart"
|
|
values={[{ label: 'Dart', value: 'dart' }]}>
|
|
|
|
<TabsPanel id="dart" label="dart">
|
|
|
|
```dart
|
|
final res = await supabase
|
|
.from('users')
|
|
.upsert({
|
|
'id': 3,
|
|
'message': 'foo',
|
|
'username': 'supabot'
|
|
})
|
|
.execute(count: CountOption.exact);
|
|
```
|
|
|
|
|
|
</TabsPanel>
|
|
|
|
</Tabs> |