172 | | When a call to a PostgreSQL 8 cursor raises an exception, all subsequent SQL |
173 | | in the same transaction fails with the error "current transaction is aborted, |
174 | | queries ignored until end of transaction block". Whilst simple use of save() |
175 | | is unlikely to raise an exception in PostgreSQL, there are many more advanced |
176 | | usage patterns which might: for example, saving objects with unique fields, |
177 | | saving using the force_insert/force_update flag, or invoking custom SQL. |
| 172 | When a call to a PostgreSQL cursor raises an exception (typically |
| 173 | ``IntegrityError``), all subsequent SQL in the same transaction fails with |
| 174 | the error "current transaction is aborted, queries ignored until end of |
| 175 | transaction block". Whilst simple use of save() is unlikely to raise an |
| 176 | exception in PostgreSQL, there are many more advanced usage patterns which |
| 177 | might: for example, saving objects with unique fields, saving using the |
| 178 | force_insert/force_update flag, or invoking custom SQL. Example:: |
191 | | Savepoints are not implemented in PostgreSQL 7. If you experience an |
192 | | IntegrityError when using PostgreSQL 7, you will need to rollback to the |
193 | | start of the transaction. |
| 187 | There are three possible solutions to this issue: |
| 188 | |
| 189 | * With any version of PostgreSQL, you can rollback to the start of the |
| 190 | transaction if you experience an exception. Example:: |
| 191 | |
| 192 | a.save() # succeeds, but may be undone by rollback |
| 193 | try: |
| 194 | b.save() # throws exception due to unique field |
| 195 | except: |
| 196 | transaction.rollback() |
| 197 | c.save() # succeeds |
| 198 | |
| 199 | Note that under the ``commit_on_success`` or ``commit_manually`` |
| 200 | decorators the rollback will also undo ``a.save()``. |
| 201 | |
| 202 | * With PostgreSQL 8 or later, you can wrap just the command which may |
| 203 | raise an exception inside savepoints, isolating the rollback. Example:: |
| 204 | |
| 205 | a.save() # succeeds, and never undone by savepoint rollback |
| 206 | try: |
| 207 | sid = transaction.savepoint() |
| 208 | b.save() # throws exception due to unique field |
| 209 | transaction.savepoint_commit(sid) |
| 210 | except: |
| 211 | transaction.savepoint_rollback(sid) |
| 212 | c.save() # succeeds |
| 213 | |
| 214 | * With PostgreSQL 8.2 or later, there is an advanced option to run |
| 215 | PostgreSQL with :ref:`database-level autocommit <ref-databases>`. |
| 216 | With this option there is no constantly open transaction, so |
| 217 | savepoints are not required to continue after catching an exception. |
| 218 | Please see the documentation for that feature, which behaves |
| 219 | differently from the standard ``autocommit`` decorator. Example:: |
| 220 | |
| 221 | a.save() # succeeds |
| 222 | try: |
| 223 | b.save() # throws exception due to unique field |
| 224 | except: |
| 225 | pass |
| 226 | c.save() # succeeds |