I come from a mysql background where if I want to insert a record I can use an INSERT IGNORE command so that I can just bung rows in and if there’s a conflict just ignore it.
Now, with a Django project I’m working on I’m using postgresql, and issue multiple saves in a loop. However, I think if I get an IntegrityError (which I’m ignoring, see code below) then I lose loads of objects that I’m trying to insert.. how should I fix this:
for thing in things:
try:
#potentially dangerous save..
obj = Thing(stuff=10)
obj.save()
except IntegrityError:
pass
If say, on the 5th go round the loop an IntegrityError happens, which objects will get inserted into the DB?
It will depend on how you manage your transaction.
On Postgres if one of query in transaction fails all subsequent query will also fail with error “current transaction is aborted, queries ignored until end of transaction block”.
To deal with it you should use transaction.savepoint_rollback in an except block.
Please refer to Django doc Handling exceptions within PostgreSQL transactions
It gives the next example