title: How can I insert multiple rows with INSERT REPLACE in peewee?
body: <p>I'm retrieving a json file from online that I want to insert into my database using peewee. The problem is that some of the rows may already exist in my database. The solution should be to either ignore or replace the duplicate rows. </p>
  
  <p>The <a href="http://peewee.readthedocs.org/en/latest/peewee/api.html" rel="nofollow"><code>InsertQuery</code></a> function supports adding multiple rows, but I cannot figure out how to either suppress errors that the instance already exists or to replace the existing instance. </p>
  
  <p>Starting with an empty database test, I run the following code</p>
  
  <pre><code>from peewee import *
  from peewee import InsertQuery
  
  database = MySQLDatabase('test', **{'password': 'researchServer', 'user': 'root'})
  
  class BaseModel(Model):
      class Meta:
          database = database
  
  class Image(BaseModel):
      url = CharField(unique=True)
  
  database.connect()
  database.create_tables([Image])
  images= [{'url': 'one'}, {'url':'two'}]
  try:
      image_entry = InsertQuery(Image, rows=images)
      image_entry.execute()
  except:
      print 'error'
  </code></pre>
  
  <p>This produces no errors and successfully adds 'one' and 'two' to my table. </p>
  
  <p>If I then run,</p>
  
  <pre><code>images= [{'url':'three'}, {'url': 'one'}, {'url':'four'}]
  try:
      image_entry = InsertQuery(Image, rows=images)
      image_entry.execute()
  except:
      print 'error'
  </code></pre>
  
  <p>The execute function throws an error and neither 'three' or 'four' get added to the database.</p>
  
  <p>I suppose one solution would be to check each row before adding it to the database, but this seems like it would be more inefficient.</p>