question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

select columns from an insert

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

I need a way to perform an upsert, and know if I performed an update or an insert afterwards. Postgrest has an open issue to support this via 200 and 201 status codes https://github.com/PostgREST/postgrest/issues/1070. For the time being though, I can infer this on the client side using the xmax column. E.g.

POST http://<supabase-db-url>.supabase.co/rest/v1/items?select=id,xmax

If xmax === "0" then the row was inserted, otherwise an update occurred. Unfortunately, there is no way to write this kind of query with the supabase client.

Describe the solution you’d like

const result = await supabase
  .from('items')
  .insert([row], { onConflict: 'unique_field', upsert: true })
  .select('id,xmax')
if (result.data[0].xmax === "0") {
  console.log('insert')
} else {
  console.log('update')
}

Describe alternatives you’ve considered

write an rpc function to wrap this logic

Additional context

an issue on the PostgREST repo discussing possible solutions here: https://github.com/PostgREST/postgrest/issues/1683

selects on insert is valuable for multiple reasons outside of my outlined use case. Any performant client will be interested in returning the minimal rows necessary after an update/insert, rather than all of them all the time. I feel like I saw an issue or discussion around the default behavior around the RETURNING clause but for the life of me I cannot find it.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
steve-chavezcommented, Dec 11, 2020

Yes, I think it should be supported. I’ll move this to postgrest-js(which supabase-js uses underlyingly).

0reactions
github-actions[bot]commented, Jan 5, 2021

🎉 This issue has been resolved in version 0.23.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL INSERT INTO SELECT Statement - W3Schools
The INSERT INTO SELECT statement copies data from one table and inserts it into another table. ... Copy only some columns from one...
Read more >
Insert into ... values ( SELECT ... FROM ... ) - Stack Overflow
Try: INSERT INTO table1 ( column1 ) SELECT col1 FROM table2. This is standard ANSI SQL and should work on any DBMS. It...
Read more >
INSERT INTO SELECT statement overview and examples
It first Selects records from a table ( Select statement); Next, it inserts into a table specified with INSERT INTO. Note: The Column...
Read more >
SQL | INSERT INTO Statement - GeeksforGeeks
SQL | INSERT INTO Statement · Only values: First method is to specify only the value of data to be inserted without the...
Read more >
SQL Server INSERT INTO SELECT By Practical Examples
This tutorial shows you how to use the SQL Server INSERT INTO SELECT statement to insert data from other tables into a table....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found