I'm trying to import multiple rows into my SQL table which has 'id' as a unique constraint, which s supposed to get rejected by SQL insert due to constraint violation.
I'm using PostgreSQL
Assuming my data are
records_to_insert = [ [2,'Jon','2018-01-11', 26] ,
[3,'Jane','2017-12-11', 27],
[4,'Bill','2018-03-23', 26] ]
and in the database I ALREADY HAVE
2,'Jon','2018-01-11', 26
So now if I do
cur = db_conn.cursor()
cur.execute("SAVEPOINT bulk_savepoint")
try:
cur.executemany("INSERT INTO mytable VALUES (%s,%s,%s,%s);", records_to_insert )
except:
cur.execute("ROLLBACK TO SAVEPOINT bulk_savepoint")
db_conn.commit()
Because the rows have one row that violates my constraint, it does not insert the rest of the rows which do not violate the id constraint and nothing gets inserted,
how can I insert the rows that do not violate the constraint without looping through the list? I have huge data that I'm going to import and looping through each element will take a long time.
Thanks.