Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在使用Django ORM的脚本中关闭自动提交_Python_Mysql_Django_Command Line - Fatal编程技术网

Python 无法在使用Django ORM的脚本中关闭自动提交

Python 无法在使用Django ORM的脚本中关闭自动提交,python,mysql,django,command-line,Python,Mysql,Django,Command Line,我有一个使用Django ORM和MySQL后端的命令行脚本。我想关闭自动提交并手动提交。就我的一生而言,我无法让它发挥作用。下面是该脚本的精简版本。每次我运行它时,都会在testtable中插入一行,我从MySQL得到这样一条警告:“一些非事务性更改的表无法回滚” 我还尝试将insert放在函数中,并手动添加Django的commit_包装器,如下所示: @transaction.commit_manually def myfunction(): cursor = connection

我有一个使用Django ORM和MySQL后端的命令行脚本。我想关闭自动提交并手动提交。就我的一生而言,我无法让它发挥作用。下面是该脚本的精简版本。每次我运行它时,都会在testtable中插入一行,我从MySQL得到这样一条警告:“一些非事务性更改的表无法回滚”

我还尝试将insert放在函数中,并手动添加Django的commit_包装器,如下所示:

@transaction.commit_manually
def myfunction():
    cursor = connection.cursor()
    cursor.execute('SET autocommit = 0')
    cursor.execute('insert into westest values (\'X\')')
    cursor.execute('rollback')

myfunction()

我还尝试在settings.py中设置DISABLE\u TRANSACTION\u MANAGEMENT=True,但没有进一步的运气。我觉得我错过了一些明显的东西。非常感谢你能给我的任何帮助。谢谢

您的表是MyISAM还是InnoDB?请记住,MyISAM不是事务性的,因此不能回滚。请参见MySQL文档中的示例:

在事务方面,MyISAM表始终有效地在autocommit=1模式下运行


是的,我很确定Django使用的是MyISAM作为默认值。我知道很明显我错过了什么。谢谢
@transaction.commit_manually
def myfunction():
    cursor = connection.cursor()
    cursor.execute('SET autocommit = 0')
    cursor.execute('insert into westest values (\'X\')')
    cursor.execute('rollback')

myfunction()