【问题标题】:Django nested transaction.atomic throw IntegrityError "SAVEPOINT does not exist" with MySQLDjango 嵌套 transaction.atomic 与 MySQL 一起抛出 IntegrityError“SAVEPOINT 不存在”
【发布时间】:2020-08-28 01:24:01
【问题描述】:

我正在尝试使嵌套的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 STARTSET AUTOCOMMIT=0。我通过查看本地 MySQL 查询日志知道这一点。

当然,我的最终测试并没有那么简单,但下面的例子显示了应该工作的概念不应该。

是我做错了什么还是 Django 的错误?

【问题讨论】:

    标签: python mysql django unit-testing transactions


    【解决方案1】:

    这似乎是我正在使用的 mysqlclient 中的一个错误。经过大量挖掘,我能够缩小范围并在StackOverflow上找到以下答案。

    TL;DR 正在通过使用不同版本的 SQL 连接器解决此问题。就我而言,我使用的是 mysqlclient 并切换到 PyMySQL 解决了问题。

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

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2017-12-13
      • 1970-01-01
      • 2018-09-19
      相关资源
      最近更新 更多