我正在尝试使嵌套的 transaction.atomic()
工作。以下代码块在第一次退出时崩溃 transaction.atomic()
并出现以下错误 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))
发生这种情况似乎是因为 Django 在测试期间未能执行 TRANSACTION START
或 SET AUTOCOMMIT=0
。我通过查看本地 MySQL 查询日志知道这一点。
当然,我的最终测试并没有那么简单,但是下面的例子展示了应该通过不工作的概念。
是我做错了什么还是 Django 的 bug?
最佳答案
这似乎是我正在使用的 mysqlclient 中的一个错误。经过大量挖掘,我能够缩小范围并在 StackOverflow 上找到以下答案。
TL;DR 这正在通过使用不同版本的 SQL 连接器来解决。就我而言,我使用的是 mysqlclient 并切换到解决了问题的 PyMySQL。
Django + MySQL - Admin Site - Add User - OperationalError - SAVEPOINT does not exist
关于python - Django 嵌套 transaction.atomic 抛出 IntegrityError "SAVEPOINT does not exist"与 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61747732/