创建数据: insert()
对表进行INSERT操作.
const { data, error } = await supabase
.from('cities')
.insert([
{ name: 'The Shire', country_id: 554 }
])
参数
-
values required
Partial
|array
插入的值. -
options optional
undefined
|reflection
No description provided.
Notes
- 默认情况下,每次运行时insert(),客户端库都会生成一个select以返回完整记录。这很方便,但如果您的策略未配置为允许select操作,它也会导致问题。如果您正在使用行级安全性并且遇到问题,请尝试将returning参数设置为minimal。
例子
创建记录
const { data, error } = await supabase
.from('cities')
.insert([
{ name: 'The Shire', country_id: 554 }
])
批量创建
const { data, error } = await supabase
.from('cities')
.insert([
{ name: 'The Shire', country_id: 554 },
{ name: 'Rohan', country_id: 555 },
])
upsert
对于 upsert,如果设置为 true,则需要将主键列包含在数据参数中,以便正确进行更新。此外,使用的主键必须是自然主键,但是, 代理主键有一些变通方法。
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 })