title: Golang: duplicate key value violates unique constraint
body: <p>I'm getting this error when I try to insert two or more register into for loop, the first one works fine but then the error appears, the database is new and I have had recreated many times</p>
  <pre><code>pq: duplicate key value violates unique constraint \&quot;movements_pkey\
  </code></pre>
  <p>I have this model of my movements</p>
  <pre><code>type Movement struct {
      ID        int        `gorm:&quot;primary_key&quot; json:&quot;id&quot;`
      Amount    float32    `json:&quot;amount&quot;`
      FkType    *int       `gorm:&quot;column:fk_type&quot; json:&quot;fk_type&quot;`
      Type      Type       `gorm:&quot;foreignkey:FkType&quot;`
      FkIncome  int        `gorm:&quot;column:fk_income&quot; json:&quot;fk_income&quot;`
      Income    Incoming   `gorm:&quot;foreignkey:FkIncome&quot;`
      FkOrder   *int       `gorm:&quot;column:fk_order&quot; json:&quot;fk_order,omitempty&quot;`
      CreatedAt *time.Time `json:&quot;created_at&quot;`
  }
  </code></pre>
  <p>And here is where I call the function to save in database (MovementCreate) into in a for loop, the first one always is created successfully but the second give me this error, where try to create a new record but apparently postgresql take the same last id</p>
  <pre><code>func (w *WebServices) CreateMovementMoneyOut(dataMovement *validators.MovementValidator) ([]models.IncomingResult, error) {
      nameFile := &quot;Movement Services&quot;
      dataWallet, err := w.wallet.FindWalletByCustomerID(dataMovement.FKCustomer)
      if err != nil {
          utils.LogService(nameFile, &quot;Wallet Find&quot;, err.Error(), &quot;error&quot;)
          return nil, err
      }
      if (dataWallet.Amount &lt;= 0) || !(*dataMovement.FkType == 2) {
          utils.LogService(nameFile, &quot;Wallet Update&quot;, &quot;Insufficients funds&quot;, &quot;error&quot;)
          return nil, fmt.Errorf(&quot;Insufficients funds&quot;)
      }
      incomingUpdate, err := w.incoming.UpdateIncomingsByFkWallet(&amp;dataWallet.ID, *dataMovement.Amount)
      var movement models.Movement
      for _, val := range incomingUpdate {
          movement.Amount = val.Amount
          movement.FkIncome = val.ID
          movement.FkType = dataMovement.FkType
          movement.FkOrder = dataMovement.FkOrder
          _, err := w.movement.MovementCreate(&amp;movement)
  
          if err != nil {
              utils.LogService(nameFile, &quot;CreateMovementMoneyOut&quot;, err.Error(), &quot;error&quot;)
              return nil, err
          }
      }
      newAmount, err := w.incoming.CalculateWalletAmount(&amp;dataWallet.ID)
      if err != nil {
          utils.LogService(nameFile, &quot;Wallet Update&quot;, err.Error(), &quot;error&quot;)
      }
      _, err = w.wallet.WalletUpdateAmount(dataWallet, newAmount)
      if err != nil {
          utils.LogService(nameFile, &quot;Wallet Update&quot;, err.Error(), &quot;error&quot;)
          return nil, err
      }
      return incomingUpdate, nil
  }
  </code></pre>
  <p>Here when save on database</p>
  <pre><code>func (s *WalletService) MovementCreate(data *Movement) (*Movement, error) {
    result := s.Create(data)
    return data, result.Error
  }
  </code></pre>
  <p>The complete response of the error in console is this</p>
  <pre><code>2021/12/08 20:57:18 /usr/src/app/pkg/datalayers/models/movement.go:45 pq: duplicate key value violates unique constraint &quot;movements_pkey&quot;
  [1.249ms] [rows:0] INSERT INTO &quot;movements&quot; (&quot;amount&quot;,&quot;fk_type&quot;,&quot;fk_income&quot;,&quot;fk_order&quot;,&quot;created_at&quot;,&quot;id&quot;) VALUES (1000.000000,2,7,1,'2021-12-08 20:57:16.173',16) RETURNING &quot;id&quot;
  </code></pre>
  <p>16 is the last id created successfully in the first iteration in the loop, and the next iteration try with the same id but the correct data of the new record</p>