title: CakePHP: Duplicate entry for key "PRIMARY' for shell script
body: <p>I am creating a real estate website that has Listing ID's attached to each listing. I am running a script via shell in CakePHP that is parsing a csv file and should be updating any listing that already exists or inserting a new one if it does not.</p>
  
  <p>The problem is that I keep getting a <code>Duplicate entry '###' for key "PRIMARY'</code> where ### is the Listing ID that is being provided by the CSV. This script is being run from the command line.</p>
  
  <p>Here's a smaller version of what my table includes:</p>
  
  <pre><code>CREATE TABLE `listings` (
    `ListingID` int(11) NOT NULL,
    `AccessibilityYN` varchar(50) DEFAULT NULL COMMENT 'AccessibilityYN',
    `BathsFull` int(6) DEFAULT NULL COMMENT 'BathsFull',
    `BathsPartial` int(6) DEFAULT NULL COMMENT 'BathsPartial',
    `BathsTotal` decimal(5,1) DEFAULT NULL COMMENT 'BathsTotal',
    `Beds` int(11) DEFAULT NULL COMMENT 'Beds',
    PRIMARY KEY (`ListingID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  </code></pre>
  
  <p>Here's my Listing Model (notice that I have <code>public $primaryKey = 'ListingID';</code>)</p>
  
  <pre><code>class Listing extends AppModel {
      public $name = 'Listing';
      public $primaryKey = 'ListingID'; 
      public $belongsTo = array(
          'Agent' =&gt; array(
              'className'    =&gt; 'Agent',
              'foreignKey'   =&gt; 'AgentID'
          )
      );
  }
  </code></pre>
  
  <p>Here's the shell I'm running via command line:</p>
  
  <pre><code>class MyShell extends AppShell {
  
      public $uses = array('Listing');
  
      public function update_imported_listings() {
  
         /***SOME CODE HERE TO GET THE $entry FROM THE CSV***/
  
          $this-&gt;Listing-&gt;ListingID = $entry['ListingID'];
  
          if(!$this-&gt;Listing-&gt;exists()){
              $this-&gt;Listing-&gt;create();
          }
  
          if($this-&gt;Listing-&gt;save($entry)){
            echo "Saved Listing";
          } else {
            echo "Listing Failed";
          }
      }
  }
  </code></pre>
  
  <p>I understand that CakePHP usually likes <code>id</code> to be the field used in the database, however I have set <code>$primaryKey = 'ListingID'</code> in my model. I've tried setting ListingID to <code>Auto Increment</code> in the DB but that didn't work.</p>
  
  <p>Anybody have any ideas? I'm fresh out.</p>