importing data using get or create - identity error 1062

Posted by hamackey on Stack Overflow See other posts from Stack Overflow or by hamackey
Published on 2013-06-27T16:11:56Z Indexed on 2013/06/27 16:21 UTC
Read the original article Hit count: 190

Filed under:

I am importing data from a mssql database into mysql. Works except when it encounters the id of a previous entry. id is unique. I need to get entries that already exist so that they can be placed in the work of the day. Error is
IntegrityError: (1062, "Duplicate entry '001355338' for key 2") This entry is already in the database. I need it entered for that day, but can not have it added to the table. It is already there.

def handle(self, *args, **options):
 59         #patients_local = Patient.objects.all()
 60         #attendings_local = Attending.objects.all()
 61         connection = pyodbc.connect("XXXXXXXXXXX") 
 62         cursor = connection.cursor()
 63         cursor.execute(COMMAND)
 64         rows = cursor.fetchall()
 65         for row in rows:
 66             # get_or_create returns (object, boolean)
 67             p, created = Patient.objects.get_or_create(
 68                 first_name = row.Firstname,
 69                 middle_name = '',
 70                 last_name = row.Lastname,
 71                 id = row.id,
 72             )

© Stack Overflow or respective owner

Related posts about django