I'm seeing a weird error in a Django unittest where, on rare occasions, attempting to save a model instance will throw the exception:
OperationalError: cannot start a transaction within a transaction
What would cause this?
The really odd thing is that this is happening in a loop like:
for i in range(10):
obj = MyModel(name='blah %i' % i)
obj.save()
and it'll get through several iterations before throwing the exception.
Since it's non-deterministic, it's incredibly frustrating to diagnose, and I have no clear idea what's happening. Since I'm using Django's standard TransactionTestCase, I would have thought this error was impossible, since the TestCase wraps each test in an atomic()
context manager to wrap all changes in a transaction. There should be nothing attempting to create another transaction.
The only other thing I could think might effect this is that my model has a post_save signal handler, and that that might be someone breaking outside of the transaction, or otherwise interfering, but I'm not sure how to test it, since I can't reliably reproduce the error.