How to know which record fails in a Postgres bulk update transaction

329 Views Asked by At

The problem

I have to update several records R1, R2, ..., Rn in a Postgres database using sqlalchemy. The operation has to be atomic; i.e., if Ri fails the whole transaction has to be rolled back.

The code is something like this:

try:
    for instance_id in payload:
        instance = get_or_404(instance_id)
        sql = build_sql_for_patch(instance, payload)
        db.session.add(sql)
    db.session.commit()
except Exception:
    db.session.rollback()
    raise ValueError(...)

In case of errors, I'd like to return a message like this:

{
   "errors": [
      {"record": 1, "reason": "Something is wrong with record 1."},
      {"record": 4, "reason": "Something is wrong with record 4."},
      ...
   ]
}

The question

Is there a way to get the Postgres transaction errors with this granularity? Ideally, I'd like to get an error for each record that fails. If this is not possible, I'd like to get the record number that fails with its proper error message.

0

There are 0 best solutions below