mirror of
https://github.com/supabase/supabase.git
synced 2026-07-02 05:44:25 +08:00
115 lines
2.5 KiB
Plaintext
115 lines
2.5 KiB
Plaintext
---
|
|
id: upsert
|
|
title: 'Upsert data: upsert()'
|
|
slug: /upsert
|
|
custom_edit_url: https://github.com/supabase/supabase/edit/master/spec/supabase_js_v2_legacy.yml
|
|
---
|
|
|
|
import Tabs from '@theme/Tabs'
|
|
import TabItem from '@theme/TabItem'
|
|
|
|
Performs an UPSERT into the table.
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('messages')
|
|
.upsert({ id: 3, message: 'foo', username: 'supabot' })
|
|
```
|
|
|
|
## Parameters
|
|
|
|
<ul className="method-list-group">
|
|
|
|
<li className="method-list-item">
|
|
<h4 className="method-list-item-label">
|
|
<span className="method-list-item-label-name">
|
|
values
|
|
</span>
|
|
<span className="method-list-item-label-badge required">
|
|
required
|
|
</span>
|
|
<span className="method-list-item-validation">
|
|
<code>Row</code> | <code>array</code>
|
|
</span>
|
|
</h4>
|
|
<div class="method-list-item-description">
|
|
|
|
The values to insert.
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
<li className="method-list-item">
|
|
<h4 className="method-list-item-label">
|
|
<span className="method-list-item-label-name">
|
|
__namedParameters
|
|
</span>
|
|
<span className="method-list-item-label-badge required">
|
|
required
|
|
</span>
|
|
<span className="method-list-item-validation">
|
|
<code>object</code>
|
|
</span>
|
|
</h4>
|
|
<div class="method-list-item-description">
|
|
|
|
No description provided.
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
## 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
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('messages')
|
|
.upsert({ id: 3, message: 'foo', username: 'supabot' })
|
|
```
|
|
|
|
### Bulk Upsert your data
|
|
|
|
```js
|
|
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`.
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('users')
|
|
.upsert({ username: 'supabot' }, { onConflict: 'username' })
|
|
```
|
|
|
|
### Return the exact number of rows
|
|
|
|
```js
|
|
const { data, error, count } = await supabase.from('users').upsert(
|
|
{
|
|
id: 3,
|
|
message: 'foo',
|
|
username: 'supabot',
|
|
},
|
|
{
|
|
count: 'exact',
|
|
}
|
|
)
|
|
```
|