<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mark Woollen</title>
    <description>The latest articles on DEV Community by Mark Woollen (@mwoollen).</description>
    <link>https://dev.to/mwoollen</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F774101%2F85a152dc-b39c-438b-b5e6-f8445b624fbc.jpeg</url>
      <title>DEV Community: Mark Woollen</title>
      <link>https://dev.to/mwoollen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mwoollen"/>
    <language>en</language>
    <item>
      <title>Supabase UPSERT</title>
      <dc:creator>Mark Woollen</dc:creator>
      <pubDate>Sat, 08 Jun 2024 15:12:52 +0000</pubDate>
      <link>https://dev.to/mwoollen/supabase-upsert-1ebc</link>
      <guid>https://dev.to/mwoollen/supabase-upsert-1ebc</guid>
      <description>&lt;p&gt;UPSERT in Supabase is intended to be a simplified combination of UPDATE and INSERT.  (If the record already exists, then it will be updated and if not, then one will be created).  &lt;/p&gt;

&lt;p&gt;However, for me, UPSERT was a bit tricky to understand and get working.  Suppose I have a table ('the_table') with primary keys: 'id' (type: uuid, default: gen_random_uuid()) and 'name' (type: text).  The &lt;a href="https://supabase.com/docs/reference/javascript/upsert" rel="noopener noreferrer"&gt;standard documentation&lt;/a&gt; indicates using "onConflict" but does not provide any useful example. I managed to find an &lt;a href="https://www.restack.io/docs/supabase-knowledge-supabase-upsert-guide" rel="noopener noreferrer"&gt;article on restack.io&lt;/a&gt; that had a brief code snippet buried in several pages down.  I adjusted to show how onConflict can reference the 'name' column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { data: retData, error: retError } = await supabase
  .from('the_table')
  .upsert({ name: 'Pablo', description: 'Helpful and friendly'}, { onConflict: 'name' }).select();
console.log("Error: ", retError )
  console.log("Data: ", retData[0].name, ", ", retData[0].description);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This snippet shows how to UPSERT a record into 'the_table' where the conflict resolution is based on the 'name' column. If a record with name ('Pablo') exists, then it will be updated; otherwise, a new record will be inserted.&lt;/p&gt;

&lt;p&gt;NEW NOTE 9/21/2024: you can also configure "NAME" table as primary key and eliminate "ID" as primary key.&lt;/p&gt;

</description>
      <category>supabase</category>
      <category>javascript</category>
      <category>upsert</category>
    </item>
  </channel>
</rss>
