title: Gorm many-to-many relationship duplicates values
body: <p>I am trying to set up a many to many relationship in my demo API, between a <code>Job</code>, and a list of skills <code>[]Skill</code>.</p>
  <p>Job Struct</p>
  <pre class="lang-golang prettyprint-override"><code>type Job struct {
      ID          string              `sql:&quot;type:uuid;primary_key;&quot;`
      Title       string              `json:&quot;title,omitempty&quot;`
      Skills      []*skill.Skill      `json:&quot;skills,omitempty&quot;gorm:&quot;many2many:job_skill;&quot;`
      CreatedAt   time.Time
      UpdatedAt   time.Time
  }
  </code></pre>
  <p>Skill Struct</p>
  <pre class="lang-golang prettyprint-override"><code>type Skill struct {
      gorm.Model
      Name string `json:&quot;name,omitempty&quot;`
  }
  </code></pre>
  <p>I then use <code>gorm.DB.AutoMigrate()</code> to generate the join table automatically.</p>
  <p>When I send a <code>POST</code> request to my API, the data is created correctly the first time around, and the join table populates as you'd expect.</p>
  <p>Example <code>POST</code> data</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;title&quot;: &quot;Senior Python Engineer&quot;,      
      &quot;skills&quot;: [{&quot;name&quot;: &quot;javascript&quot;}, {&quot;name&quot;: &quot;python&quot;}]
  }
  </code></pre>
  <p>But then when I send a <code>PATCH</code> request to add a new skill, it duplicates the skill in the skills table and then creates a new record in the join table for the skills that already exist.</p>
  <p>Example <code>PATCH</code> data</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;title&quot;: &quot;Lead Engineer&quot;,      
      &quot;skills&quot;: [{&quot;name&quot;: &quot;javascript&quot;}, {&quot;name&quot;: &quot;python&quot;}, {&quot;name&quot;: &quot;management&quot;}]
  }
  </code></pre>
  <p>Doing a get request for the data will show the following:</p>
  <pre class="lang-json prettyprint-override"><code>{
      &quot;title&quot;: &quot;Lead Engineer&quot;,      
      &quot;skills&quot;: [{&quot;name&quot;: &quot;javascript&quot;}, {&quot;name&quot;: &quot;python&quot;}, {&quot;name&quot;: &quot;javascript&quot;}, {&quot;name&quot;: &quot;python&quot;}, {&quot;name&quot;: &quot;management&quot;}]
  }
  </code></pre>
  <p>I have also tried setting <code>gorm:&quot;unique&quot;</code> on the skill struck <code>Name</code> but when adding a new Skill it fails as it says the other two already exist, which is good but then won't add the new one.</p>
  <p>I am assuming I can only send back new values? Not the entire list?</p>
  <p>Some of my Go code for clarity</p>
  <pre class="lang-golang prettyprint-override"><code>func GetJobs(w http.ResponseWriter, r *http.Request) {
      j := &amp;[]Job{}
      o := database.DB.Preload(&quot;Skills&quot;).Find(&amp;j)
  
      render.JSON(w, r, o)
  }
  
  func UpdateJob(w http.ResponseWriter, r *http.Request) {
      j := &amp;Job{}
      err := json.NewDecoder(r.Body).Decode(j)
      if err != nil {
          return
      }
      o := database.DB.Model(&amp;j).Where(&quot;id = ?&quot;, j.ID).Update(&amp;j)
  
      render.JSON(w, r, o)
  }
  </code></pre>