UrK
UrK

Reputation: 2300

Django nested transaction.atomic throw IntegrityError "SAVEPOINT does not exist" with MySQL

I am trying to make nested transaction.atomic() work. The following block of code crashes when exiting first transaction.atomic() with the following error MySQLdb._exceptions.OperationalError: (1305, 'SAVEPOINT s4568333760_x1 does not exist')

from django.contrib.auth.models import User
from django.test import TransactionTestCase
from django.db import transaction

class FooTest(TransactionTestCase):
    def test_bar(self):
        with transaction.atomic():
            with transaction.atomic():
                u = User.objects.create_user(username="abc", password="pass")
                print("created user: {}".format(u.username))

It seems like this happens due to the fact that Django fails to execute TRANSACTION START or SET AUTOCOMMIT=0 during the test. I know this by looking at local MySQL query log.

Of course, my final test is not that simple, but the following example shows the concept which should work by does not.

Am I doing something wrong or is this a bug of Django?

Upvotes: 3

Views: 933

Answers (1)

UrK
UrK

Reputation: 2300

It seems like it's a bug in mysqlclient I am using. After a lot of digging, I was able to narrow it down and find the following answer on StackOverflow.

TL;DR This is being resolved by using different version of SQL connector. In my case, I was using mysqlclient and switched to PyMySQL which solved the problem.

Django + MySQL - Admin Site - Add User - OperationalError - SAVEPOINT does not exist

Upvotes: 2

Related Questions