トランザクションがコミットされたときに IntegrityError 例外が発生する可能性があることを適切に処理したいのですが、一見すると、それを実装するための 2 つの選択肢が見えますが、両方が正しいかどうかはわかりません。
注:PostgreSQLを使用しています
# alternative one
try:
with transaction.commit_on_success():
# db operation 1
# db operation 2
except IntegrityError:
transaction.rollback()
#alternative two
with transaction.commit_manually():
sid = transaction.savepoint()
# db operation 1
# db operation 2
try:
transaction.savepoint_commit(sid)
except IntegrityError:
transaction.savepoint_rollback(sid)