---
id: upsert
title: "Upsert data: upsert()"
slug: upsert
custom_edit_url: https://github.com/supabase/supabase/edit/master/web/spec/supabase.yml
---
import Tabs from '@theme/Tabs';
import TabsPanel from '@theme/TabsPanel';
Performs an UPSERT into the table.
```js
const { data, error } = await supabase
.from('messages')
.upsert({ id: 3, message: 'foo', username: 'supabot' })
```
## Parameters
-
values
required
Partial | array
The values to insert.
-
__namedParameters
required
object
No description provided.
Properties
-
returning
required
minimal | representation
By default the new record is returned. Set this to 'minimal' if you don't need this value.
-
onConflict
required
undefined | string
By specifying the `on_conflict` query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint.
-
ignoreDuplicates
required
boolean
Specifies if duplicate rows should be ignored and not inserted.
-
count
required
null | exact | planned | estimated
Count algorithm to use to count rows in a table.
## 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'
})
```