Opened 8 years ago
Last modified 8 years ago
#27504 closed Bug
Cannot Make ORM Queries After an Error and Rollback In Non-autocommit Mode — at Initial Version
Reported by: | Mark Young | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.10 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In non-autocommit mode, after an error has occurred (specifcally, IntegrityError and ValidationErrors are known to cause the issue, but I haven't fully investigated which errors can cause this), the db connection's needs_rollback attribute is set to True. When the flag is set to True, no further orm queries can be made to the database. Once this has happened, there appears to be no way to clean up the transaction, as a transaction.rollback() does not unset this flag.
An example failing scenario:
transaction.set_autocommit(False) r1 = Reporter.objects.create(first_name="Archibald", last_name="Haddock") r2 = Reporter(first_name="Cuthbert", last_name="Calculus", id=r1.id) try: r2.save(force_insert=True) except IntegrityError: transaction.rollback() # This raises a TransactionManagementError last_reporter = Reporter.objects.last()
This ticket is related to https://code.djangoproject.com/ticket/26340 and solves a subset of the scenarios affected by it.