title: Yii - CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5375' for key 'PRIMARY'
body: <p>I am working with Yii 1.1.x and get the following error when performing an insert.</p>
  
  <pre><code>CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: 
  Integrity constraint violation: 1062 Duplicate entry '5375' for key 'PRIMARY'
  </code></pre>
  
  <p>This happens on a table called 'competition_prizes' - there is only one primary key called 'id' (there are no other indexes or anything like that).</p>
  
  <p>I can see that there is one row with the id of 5375 so that entry does already exist (as per the insert query).</p>
  
  <p>The controller code is as follows has some functionality within the afterSave() functionality.</p>
  
  <pre><code>protected function setPrizes($prizes, $prize_type)
  {
      if(!empty($prizes) &amp;&amp; is_array($prizes))
      {
              $prize_model = CompetitionPrizes::model();
              $competition_id = $this-&gt;competition_id;
              $prize_model-&gt;deleteAll('competition_id =:competition_id AND  prize_type = :prize_type ',array(
              ':competition_id'=&gt; $competition_id,
              ':prize_type'=&gt; $prize_type
              ));
  
              foreach($prizes as $prize_position =&gt; $prize_desc) :
                  $prize_model-&gt;setIsNewRecord(true);
                  $prize_model-&gt;setAttributes(compact('competition_id', 'prize_position', 'prize_type','prize_desc'));
                  $prize_model-&gt;save();
              endforeach;
      }   
  }
  </code></pre>
  
  <p>Any ideas of how to get around the error - please note I'm new to Yii so be gentle :)</p>