Reputation: 1135
Based on the docs, inserting a new record
const { error } = await supabase
.from('countries')
.insert({ name: 'Denmark' })
returns
{
"status": 201,
"statusText": "Created"
}
For columns that are Is Identity
, it automatically assigns a sequential unique number to the column.
How to get the ID of the newly inserted record on Supabase with JavaScript client?
Upvotes: 22
Views: 18867
Reputation: 3933
I think you needed this:
const { data, error } = await supabase
.from('countries')
.insert({ name: 'Denmark', {returning: 'minimal'}})
.select()
With this the data will only contain the id
of the created country.
Upvotes: -1
Reputation: 7262
As per the code comments in node_modules/@supabase/postgrest-js/src/PostgrestQueryBuilder.ts
.
Perform an INSERT into the table or view. By default, inserted rows are not returned. To return it, chain the call with .select().
Just add .select()
at the end.
await supabase
.from('countries')
.insert({ name: 'Denmark', {returning: 'minimal'}})
.select()
Upvotes: 1
Reputation: 18680
You can add .select()
after your insert statement in order to get the newly inserted record.
const { data, error } = await supabase
.from('countries')
.insert({ name: 'Denmark' })
.select()
You will see the inserted record inside data
variable. Note that retrieving the inserted record requires you to have not only insert permission, but select permission on RLS policies.
On a side note, you can click on the buttons on the right of the docs to view different use cases. You can see this example on Create a record and return it
example.
Upvotes: 52