I'm trying to run the following Django unittest:
class MyModelTests(TestCase):
def test_failed_duplicate(self):
m = MyModel.objects.create(a='a', b='a')
with self.assertRaises(IntegrityError):
MyModel.objects.create(a='a', b='b')
with self.assertRaises(IntegrityError):
MyModel.objects.create(a='a', b='c')
with self.assertRaises(IntegrityError):
MyModel.objects.create(a='a', b='d')
There are several tests that all should fail due to violating a uniqueness constraint on field a. (I've obfuscated the assertions a bit, but they all test different values of a that should fail.)
However, when running I get:
Traceback (most recent call last):
File "...", line 21, in test_failed_duplicate
MyModel.objects.create(a='a', b='c')
TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
What am I missing?