Upsert data
Perform an UPSERT on the table or view. Depending on the column(s) passed
to onConflict
, .upsert()
allows you to perform the equivalent of
.insert()
if a row with the corresponding onConflict
columns doesn't
exist, or if it does exist, perform an alternative action depending on
ignoreDuplicates
.
- Primary keys must be included in
values
to use upsert.
Parameters
- valuesREQUIREDobject[]
The values to upsert with. Pass an object to upsert a single row or an array to upsert multiple rows.
- optionsOptionalobject
Named parameters
countOptional"exact" | "planned" | "estimated"Count algorithm to use to count upserted rows. `"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the hood. `"planned"`: Approximated but fast count algorithm. Uses the Postgres statistics under the hood. `"estimated"`: Uses exact count for low numbers and planned count for high numbers.
defaultToNullOptionalbooleanMake missing fields default to `null`. Otherwise, use the default value for the column. This only applies when inserting new rows, not when merging with existing rows under `ignoreDuplicates: false`. This also only applies when doing bulk upserts.
ignoreDuplicatesOptionalbooleanIf `true`, duplicate rows are ignored. If `false`, duplicate rows are merged with existing rows.
onConflictOptionalstringComma-separated UNIQUE column(s) to specify how duplicate rows are determined. Two rows are duplicates if all the `onConflict` columns are equal.
const { data, error } = await supabase
.from('countries')
.upsert({ id: 1, name: 'Albania' })
.select()