title: Duplicate entry for key 'PRIMARY' in mysql
body: <p>I have a table called <code>tbl_jobs</code> that stores the meta data of some background jobs running in the application. The schema is like :</p>
  
  <pre><code>CREATE TABLE `tbl_jobs` (
    `type` varchar(30) NOT NULL DEFAULT '',
    `last_run_on` datetime NOT NULL,
    `records_updated` text,
    PRIMARY KEY (`type`,`last_run_on`),
    UNIQUE KEY `index2` (`type`,`last_run_on`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
  </code></pre>
  
  <p>Whenever a job runs it makes an entry in the table with the <code>type</code> which is a unique identifier for different jobs, <code>run time</code> and the <code>records updated</code> in that run.</p>
  
  <p>There are two different jobs that run at same time with types : <code>MAILER_UNLOCKED_REWARDS</code>
  and <code>MAILER_ALMOST_UNLOCKED</code>.</p>
  
  <p>When these jobs try to insert their entries with the same timestamp only one of them gets inserted and the other throws a <strong>Duplicate Entry for key</strong> error.</p>
  
  <p>For instance the two jobs ran the following :</p>
  
  <pre><code>INSERT INTO tbl_jobs
              (type,
               last_run_on,
               records_updated)
  VALUES     ('MAILER_ALMOST_UNLOCKED',
              '2012-08-22 19:10:00',
              'f8a35230fb214989ac75bf11c085aa28:b591426df4f340ecbce5a63c2a5a0174')
  </code></pre>
  
  <p>that ran successfully but when the second job ran the insert command</p>
  
  <pre><code>INSERT INTO tbl_jobs
              (type,
               last_run_on,
               records_updated)
  VALUES     ('MAILER_UNLOCKED_REWARDS',
              '2012-08-22 19:10:00',
              '8a003e8934c07f040134c30959c40009:59bcc21b33a0466e8e5dc50443beb945')
  </code></pre>
  
  <p>It threw the error</p>
  
  <pre><code>Duplicate entry 'M-2012-08-22 19:10:00' for key 'PRIMARY'
  </code></pre>
  
  <p>The primary key is combination of <code>type</code> and <code>last_run_on</code> columns.</p>
  
  <p>If I delete the entry for the first job the insertion succeeds, i.e it is asking for <code>timestamp</code> alone to be unique.</p>
  
  <p>However the conflict for the same <code>timestamp</code> occurs only between these two jobs.There are other jobs that get inserted for the same <code>timestamp</code>.</p>
  
  <p>Any ideas on what could be the issue?</p>