title: MySQL INSERT ... ON DUPLICATE KEY UPDATE with django 1.4 for bulk insert
body: <p>I am having issues figuring out MySQL INSERT ... ON DUPLICATE KEY UPDATE with django 1.4. </p>
  
  <p>The table that I am trying to insert records has a 2 column(composite) unique key. Records that I am receiving is from a 3rd party source and values will change over time except for those fields that makes the unique key set. I am receiving 1 ~ 5k records at a time, and would need to </p>
  
  <p>Currently I am using Model.objects.bulk_create to bulk insert, performance is really amazing as it issues generally one query no matter how big the record set is. However, as my records can change over time on the 3rd party end, I need to perform the MySQL INSERT ... ON DUPLICATE KEY UPDATE query on the recordset. </p>
  
  <p>I am planning to write raw SQL statements and execute using something like here: </p>
  
  <pre><code>sql = "MySQL INSERT ... ON DUPLICATE KEY UPDATE"
  
  raw_insert(sql)
  
  def raw_insert(sql):
      from django.db import connection, transaction
      cursor = connection.cursor()
  
      # Data modifying operation - commit required
      cursor.execute(sql)
      transaction.commit_unless_managed()
  
      return 1
  </code></pre>
  
  <p>Wondering if there is a better solution to my problem. Also how would I sanitize the field values for raw insert?</p>