I am having issues figuring out MySQL INSERT … ON DUPLICATE KEY UPDATE with django 1.4.
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
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.
I am planning to write raw SQL statements and execute using something like here:
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
Wondering if there is a better solution to my problem. Also how would I sanitize the field values for raw insert?
So I created a custom manager. Here is the manager:
And a sample model:
And sample implementation:
Here is the gist.