I have a command line script that uses Django ORM and MySQL server modules. I want to turn off auto-messaging and do it manually. For life, I cannot make it work. Below is the version of the script. The row is inserted into the test table every time I run it, and I get this warning from MySQL: "Some non-transactional tables cannot be undone."
import os
import sys
django_dir = os.path.abspath(os.path.normpath(os.path.join(os.path.dirname(__file__), '..')))
sys.path.append(django_dir)
os.environ['DJANGO_DIR'] = django_dir
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
from django.core.management import setup_environ
from myproject import settings
setup_environ(settings)
from django.db import transaction, connection
cursor = connection.cursor()
cursor.execute('SET autocommit = 0')
cursor.execute('insert into testtable values (\'X\')')
cursor.execute('rollback')
I also tried putting the insert in a function and adding a Django commit_manual wrapper, for example:
@transaction.commit_manually
def myfunction():
cursor = connection.cursor()
cursor.execute('SET autocommit = 0')
cursor.execute('insert into westest values (\'X\')')
cursor.execute('rollback')
myfunction()
I also tried setting DISABLE_TRANSACTION_MANAGEMENT = True in settings.py, without further luck. I feel like I'm missing something obvious. Any help you can give me is greatly appreciated. Thank!