<?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: Roman</title>
    <description>The latest articles on DEV Community by Roman (@kochurovro).</description>
    <link>https://dev.to/kochurovro</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%2F261852%2F6b9505a0-4adc-4080-b601-916ba4af6eb6.jpeg</url>
      <title>DEV Community: Roman</title>
      <link>https://dev.to/kochurovro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kochurovro"/>
    <language>en</language>
    <item>
      <title>The Wizard's Guide to GORM and PostgreSQL: Upsert with Ease</title>
      <dc:creator>Roman</dc:creator>
      <pubDate>Wed, 05 Jun 2024 11:13:45 +0000</pubDate>
      <link>https://dev.to/kochurovro/conquering-postgresql-challenges-upserting-records-with-gorm-magic-1mnd</link>
      <guid>https://dev.to/kochurovro/conquering-postgresql-challenges-upserting-records-with-gorm-magic-1mnd</guid>
      <description>&lt;p&gt;Interesting story. You have two PostgreSQL tables related to each other in a one-to-many relationship.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Availability struct {
    ID        int64    `gorm:"column:id;primaryKey"`
    ProductID int64    `gorm:"column:product_id"`
    Enabled   bool     `gorm:"column:enabled;not null"`
    Dates     []*Dates `gorm:"foreignKey:AvailabilityID"`
}

type Dates struct {
    ID             int64  `gorm:"column:id;primaryKey"`
    AvailabilityID int64  `gorm:"column:availability_id;foreignKey:Availability" json:"availability_id"`
    Date           string `gorm:"column:date"`
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And such an index product_availability_dates_uindex(AvailabilityID, date) in the Dates table. You need to update the Enabled field in the Availability table using GORM. If such a record does not exist, create it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (that *AvailabilityRepo) Upsert(ctx context.Context, pa []*model.ProductAvailability) error {
    for _, a := range pa {
        tx := that.db.WithContext(ctx).Model(&amp;amp;model.ProductAvailability{}).
            Where("product_id = ?", a.ProductID).
            Update("enabled = ?", a.Enabled)
        if tx.Error != nil {
            return fmt.Errorf("%w: failed to update product availability", tx.Error)
        }

        if tx.RowsAffected == 0 {
            if err := tx.Create(&amp;amp;a).Error; err != nil {
                return fmt.Errorf("%w: failed to create product availability", err)
            }
        }

        return nil
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Problem 1. We need to return the ID if the field is updated. Currently, the ID is not being returned. We try to pass the instance of the object directly in the Model. This method seems to work, but not for me.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (that *AvailabilityRepo) Upsert(ctx context.Context, pa []*model.ProductAvailability) error {
    for _, a := range pa {
        tx := that.db.WithContext(ctx).Model(&amp;amp;a).
            Where("product_id = ?", a.ProductID).
            Update("enabled = ?", a.Enabled)
        if tx.Error != nil {
            return fmt.Errorf("%w: failed to update product availability", tx.Error)
        }

        if tx.RowsAffected == 0 {
            if err := tx.Create(&amp;amp;a).Error; err != nil {
                return fmt.Errorf("%w: failed to create product availability", err)
            }
        }

        return nil
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why the above update did not work for me. The first reason, it makes me want to wash my hands after such an approach. The second reason is that the tables are connected through a unique index. And GORM, by some internal magic, returns an error because the uniqueness index is triggered. You didn't touch the dates field, but the error will be related to this index.&lt;/p&gt;

&lt;p&gt;How I solved the problem for myself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;func (that *AvailabilityRepo) Upsert(ctx context.Context, pa []*model.ProductAvailability) error {
    for _, a := range pa {
        idContainer := model.ProductAvailability{}
        tx := updatesBuilder(that.db.WithContext(ctx).Model(&amp;amp;idContainer).
            Clauses(clause.Returning{Columns: []clause.Column{
                {Name: "id"},
            }}).
            Where("product_id = ?", a.ProductID), deprecate, a)
        if tx.Error != nil {
            return fmt.Errorf("%w: failed to update product availability", tx.Error)
        }

        if tx.RowsAffected == 0 {
            if err := tx.Create(&amp;amp;a).Error; err != nil {
                return fmt.Errorf("%w: failed to create product availability", err)
            }
        }

        if a.ID == 0 {
            a.ID = idContainer.ID
        }
    }

    return nil
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
