title: Postgres says key already exists although it doesn't
body: <p>My Python application uses Psycopg2 to insert content from a web scraper to a PostgreSQL database. Psycopg2 complains that a certain primary key already exists even though it clearly doesn't.</p>
  
  <p>Error:</p>
  
  <pre><code>psycopg2.IntegrityError: duplicate key value violates unique constraint "my_table_pkey"
  DETAIL:  Key (id)=(12345) already exists.
  </code></pre>
  
  <p>Query:</p>
  
  <pre><code>SELECT * FROM my_table where id=12345;
  -- 0 rows returned
  </code></pre>
  
  <p>What is going on here?</p>
  
  <p><strong>Edit for background:</strong></p>
  
  <p>Basically, what the code does is scrape a discussion forum, loops over each page in each discussion thread and inserts some data from each thread into Postgres. The general structure of the code is outlined below. Note that <code>get</code> returns a well formatted data structure for each thread.</p>
  
  <pre><code>import psycopg2
  
  base_url 'http://someforum.com'
  conn = psycopg2.connect('dbname=mydb user=me')
  
  for i in range(10000):
      thread = get('{}/'{}.format(base_url, i)
      for page in thread:
          sql = 'INSERT INTO my_table (id, foo, bar) VALUES(%s, %s, %s);'
          values = [page['id'], page['foo'], page['bar']]
          cur.execute(sql, values)
      conn.commit()
  cur.close()
  conn.close()
  </code></pre>