From 2742901ac210361bc2c4b662870d35a1be5a142c Mon Sep 17 00:00:00 2001 From: Mark Young Date: Wed, 23 Nov 2016 08:42:19 -0500 Subject: [PATCH] Fixed #27504 -- Allowed using the ORM after an error and rollback when autocommit is off. --- django/db/backends/base/base.py | 2 +- tests/transactions/tests.py | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/django/db/backends/base/base.py b/django/db/backends/base/base.py index 81f53685ca..38712bd4bf 100644 --- a/django/db/backends/base/base.py +++ b/django/db/backends/base/base.py @@ -267,7 +267,7 @@ class BaseDatabaseWrapper(object): self._rollback() # A successful rollback means that the database connection works. self.errors_occurred = False - + self.needs_rollback = False self.run_on_commit = [] def close(self): diff --git a/tests/transactions/tests.py b/tests/transactions/tests.py index dcfd30fcaa..b3fdc7f915 100644 --- a/tests/transactions/tests.py +++ b/tests/transactions/tests.py @@ -443,8 +443,28 @@ class AtomicMiscTests(TransactionTestCase): # This is expected to fail because the savepoint no longer exists. connection.savepoint_rollback(sid) - @skipIf(connection.features.autocommits_when_autocommit_is_off, - "This test requires a non-autocommit mode that doesn't autocommit.") + +@skipIf( + connection.features.autocommits_when_autocommit_is_off, + "This test requires a non-autocommit mode that doesn't autocommit." +) +class NonAutocommitTests(TransactionTestCase): + + available_apps = [] + + def test_orm_query_after_error_and_rollback(self): + """ + ORM queries are allowed after an error and a rollback in non-autocommit + mode (#27504). + """ + 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) + with self.assertRaises(IntegrityError): + r2.save(force_insert=True) + transaction.rollback() + Reporter.objects.last() + def test_orm_query_without_autocommit(self): """#24921 -- ORM queries must be possible after set_autocommit(False).""" transaction.set_autocommit(False)