title: How can I append to a many-to-many relation in Gorm without upserting the already-existing associated rows?
body: <p>How can I associate a model with already-existing other models in a many-to-many association without having GORM execute queries to UPSERT the already-existing models?</p>
  <p>Example:</p>
  <p>Suppose I have two GORM models in a many-to-many association with each other:</p>
  <pre><code>type A struct {
      ID int `gorm:&quot;primaryKey&quot;`
      Bs []B `gorm:&quot;many2many:a_bs;&quot;`
  }
  
  type B struct {
      ID int `gorm:&quot;primaryKey&quot;`
      As []A `gorm:&quot;many2many:a_bs;&quot;`
  }
  </code></pre>
  <p>Suppose I create two As:</p>
  <pre><code>a1 := A{}
  db.Create(&amp;a1)
  
  a2 := A{}
  db.Create(&amp;a2)
  </code></pre>
  <p>This will result in the following queries as expected:</p>
  <pre><code>INSERT INTO &quot;as&quot; DEFAULT VALUES RETURNING &quot;id&quot;
  INSERT INTO &quot;as&quot; DEFAULT VALUES RETURNING &quot;id&quot;
  </code></pre>
  <p>And suppose I then want to create a B and associate it with the As:</p>
  <pre><code>b := B{
      As: []A{a1, a2},
  }
  db.Create(&amp;b)
  </code></pre>
  <p>This will result in the following queries:</p>
  <pre><code>INSERT INTO &quot;as&quot; (&quot;id&quot;) VALUES (3),(4) ON CONFLICT DO NOTHING RETURNING &quot;id&quot;
  INSERT INTO &quot;a_bs&quot; (&quot;b_id&quot;,&quot;a_id&quot;) VALUES (3,3),(3,4) ON CONFLICT DO NOTHING
  INSERT INTO &quot;bs&quot; DEFAULT VALUES RETURNING &quot;id&quot;
  </code></pre>
  <p>How can I eliminate the first query, which unnecessarily upserts the already-inserted A models?</p>
  <p>Using</p>
  <pre><code>b := B{}
  db.Create(&amp;b)
  db.Model(&amp;b).Association(&quot;As&quot;).Append([]A{a1, a2})
  </code></pre>
  <p>for the second bit of code instead has the same effect. Adding Omit(&quot;As&quot;) clauses will cancel the entire insertion.</p>