title: "Integrity constraint violation: 1062 Duplicate entry" - but no duplicate rows
body: <p>I'm converting an app from native mysqli calls to PDO.  Running into an error when attempting to insert a row into a table with a foreign key constraint.   </p>
  
  <p><strong>Note:</strong> this is a simplified test case and should not be copy/pasted into a production environment.  </p>
  
  <p><strong>Info</strong> PHP 5.3, MySQL 5.4</p>
  
  <p>First, here are the tables:</p>
  
  <pre><code>CREATE TABLE `z_one` (
    `customer_id` int(10) unsigned NOT NULL DEFAULT '0',
    `name_last` varchar(255) DEFAULT NULL,
    `name_first` varchar(255) DEFAULT NULL,
    `dateadded` datetime DEFAULT NULL,
    PRIMARY KEY (`customer_id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  
  INSERT INTO `z_one` VALUES (1,'Khan','Ghengis','2014-12-17 10:43:01');
  
  CREATE TABLE `z_many` (
    `order_id` varchar(15) NOT NULL DEFAULT '',
    `customer_id` int(10) unsigned DEFAULT NULL,
    `dateadded` datetime DEFAULT NULL,
    PRIMARY KEY (`order_id`),
    KEY `order_index` (`customer_id`,`order_id`),
    CONSTRAINT `z_many_ibfk_1` FOREIGN KEY (`customer_id`) REFERENCES `z_one` (`customer_id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p>Or if you prefer, </p>
  
  <pre><code>mysql&gt; describe z_one;
  +-------------+------------------+------+-----+---------+-------+
  | Field       | Type             | Null | Key | Default | Extra |
  +-------------+------------------+------+-----+---------+-------+
  | customer_id | int(10) unsigned | NO   | PRI | 0       |       |
  | name_last   | varchar(255)     | YES  |     | NULL    |       |
  | name_first  | varchar(255)     | YES  |     | NULL    |       |
  | dateadded   | datetime         | YES  |     | NULL    |       |
  +-------------+------------------+------+-----+---------+-------+
  4 rows in set (0.00 sec)
  
  
  mysql&gt; describe z_many;
  +-------------+------------------+------+-----+---------+-------+
  | Field       | Type             | Null | Key | Default | Extra |
  +-------------+------------------+------+-----+---------+-------+
  | order_id    | varchar(15)      | NO   | PRI |         |       |
  | customer_id | int(10) unsigned | YES  | MUL | NULL    |       |
  | dateadded   | datetime         | YES  |     | NULL    |       |
  +-------------+------------------+------+-----+---------+-------+
  3 rows in set (0.00 sec)
  </code></pre>
  
  <p>Next, here is the query: </p>
  
  <pre><code>    $order_id = '22BD24';
      $customer_id = 1;
  
      try 
      {
          $q = "
              INSERT INTO 
                  z_many 
                  (
                      order_id,
                      customer_id,
                      dateadded
                  )
              VALUES 
                  (
                      :order_id,
                      :customer_id,
                      NOW()
                  )
          ";
          $stmt = $dbx_pdo-&gt;prepare($q);
          $stmt-&gt;bindValue(':order_id', $order_id, PDO::PARAM_STR);
          $stmt-&gt;bindValue(':customer_id', $customer_id, PDO::PARAM_INT);
          $stmt-&gt;execute();
  
      } catch(PDOException $err) {
          // test case only.  do not echo sql errors to end users.
          echo $err-&gt;getMessage();
      }   
  </code></pre>
  
  <p>This results in the following PDO error: </p>
  
  <blockquote>
    <p>SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
    '22BD24' for key 'PRIMARY'</p>
  </blockquote>
  
  <p>The same query works fine when handled by <code>mysqli</code>.  Why is PDO rejecting the INSERT with a 'duplicate entry' message when there aren't any duplicates found?</p>