mirror of
https://github.com/supabase/supabase.git
synced 2026-10-07 02:15:05 +03:00
274 lines
4.7 KiB
Plaintext
274 lines
4.7 KiB
Plaintext
---
|
|
id: insert
|
|
title: "Create data: insert()"
|
|
slug: insert
|
|
---
|
|
|
|
import Tabs from '@theme/Tabs';
|
|
import TabItem from '@theme/TabItem';
|
|
|
|
Performs an INSERT into the table.
|
|
|
|
<Tabs
|
|
defaultValue="js"
|
|
groupId="libraries"
|
|
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
|
|
|
|
<TabItem value="js">
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('cities')
|
|
.insert([
|
|
{ name: 'The Shire', country_id: 554 }
|
|
])
|
|
```
|
|
|
|
|
|
</TabItem>
|
|
<TabItem value="py">
|
|
|
|
```py
|
|
# Not yet implemented
|
|
```
|
|
|
|
|
|
</TabItem>
|
|
|
|
</Tabs>
|
|
|
|
## 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>Partial</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>
|
|
|
|
<ul className="method-list-group">
|
|
<h5 class="method-list-title method-list-title-isChild expanded">Properties</h5>
|
|
|
|
<li className="method-list-item">
|
|
<h4 className="method-list-item-label">
|
|
<span className="method-list-item-label-name">
|
|
upsert
|
|
</span>
|
|
<span className="method-list-item-label-badge required">
|
|
required
|
|
</span>
|
|
<span className="method-list-item-validation">
|
|
<code>boolean</code>
|
|
</span>
|
|
</h4>
|
|
<div class="method-list-item-description">
|
|
|
|
If `true`, performs an UPSERT.
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
|
|
<li className="method-list-item">
|
|
<h4 className="method-list-item-label">
|
|
<span className="method-list-item-label-name">
|
|
returning
|
|
</span>
|
|
<span className="method-list-item-label-badge required">
|
|
required
|
|
</span>
|
|
<span className="method-list-item-validation">
|
|
<code>minimal</code> | <code>representation</code>
|
|
</span>
|
|
</h4>
|
|
<div class="method-list-item-description">
|
|
|
|
By default the new record is returned. Set this to 'minimal' if you don't need this value.
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
|
|
<li className="method-list-item">
|
|
<h4 className="method-list-item-label">
|
|
<span className="method-list-item-label-name">
|
|
onConflict
|
|
</span>
|
|
<span className="method-list-item-label-badge required">
|
|
required
|
|
</span>
|
|
<span className="method-list-item-validation">
|
|
<code>undefined</code> | <code>string</code>
|
|
</span>
|
|
</h4>
|
|
<div class="method-list-item-description">
|
|
|
|
By specifying the `on_conflict` query parameter, you can make UPSERT work on a column(s) that has a UNIQUE constraint.
|
|
|
|
</div>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
### Create a record
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultValue="js"
|
|
groupId="libraries"
|
|
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
|
|
|
|
<TabItem value="js">
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('cities')
|
|
.insert([
|
|
{ name: 'The Shire', country_id: 554 }
|
|
])
|
|
```
|
|
|
|
|
|
</TabItem>
|
|
<TabItem value="py">
|
|
|
|
```py
|
|
# Not yet implemented
|
|
```
|
|
|
|
|
|
</TabItem>
|
|
|
|
</Tabs>
|
|
|
|
### Bulk create
|
|
|
|
|
|
|
|
<Tabs
|
|
defaultValue="js"
|
|
groupId="libraries"
|
|
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
|
|
|
|
<TabItem value="js">
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('cities')
|
|
.insert([
|
|
{ name: 'The Shire', country_id: 554 },
|
|
{ name: 'Rohan', country_id: 555 },
|
|
])
|
|
```
|
|
|
|
|
|
</TabItem>
|
|
<TabItem value="py">
|
|
|
|
```py
|
|
# Not yet implemented
|
|
```
|
|
|
|
|
|
</TabItem>
|
|
|
|
</Tabs>
|
|
|
|
### Upsert
|
|
|
|
For upsert, if set to true, primary key columns would need to be included
|
|
in the data parameter in order for an update to properly happen. Also, primary keys
|
|
used must be natural, not surrogate. There are however,
|
|
[workarounds](https://github.com/PostgREST/postgrest/issues/1118)
|
|
for surrogate primary keys.
|
|
|
|
|
|
<Tabs
|
|
defaultValue="js"
|
|
groupId="libraries"
|
|
values={[{ label: 'JavaScript', value: 'js' },{ label: 'Python', value: 'py' }]}>
|
|
|
|
<TabItem value="js">
|
|
|
|
```js
|
|
const { data, error } = await supabase
|
|
.from('cities')
|
|
.insert(
|
|
[
|
|
{ name: 'The Shire', country_id: 554 },
|
|
{ name: 'Rohan', country_id: 555 },
|
|
{ name: 'City by the Bay', country_id:840}
|
|
],
|
|
{ upsert: true })
|
|
```
|
|
|
|
|
|
</TabItem>
|
|
<TabItem value="py">
|
|
|
|
```py
|
|
# Not yet implemented
|
|
```
|
|
|
|
|
|
</TabItem>
|
|
|
|
</Tabs> |