michiya / django-pyodbc-azure

Django backend for Microsoft SQL Server and Azure SQL Database using pyodbc
https://pypi.python.org/pypi/django-pyodbc-azure
BSD 3-Clause "New" or "Revised" License
321 stars 140 forks source link

with transaction.atomic() not working for Azure SQL Database #208

Open RRaideRR opened 5 years ago

RRaideRR commented 5 years ago

I usedjango-pyodbc-azure 2.1.0.0 for the connection with an Azure SQL database which works fine.

When I understand the documentation of django-pyodbc-azure correctly, transactions should be supported.

However, this code immediately updates the row. I would expect, that the row is updated after 20 seconds.

from django.db import transaction
from myapp.models import MyModel
import time

with transaction.atomic():
    MyModel.objects.filter(id=1).update(my_field='Test')
    time.sleep(20)

Am I doing something wrong? Do I need to specifiy certain settings on the Azure SQL database?

My current settings.py

'azure_reporting': {
    'ENGINE': 'sql_server.pyodbc',
    'NAME': 'reporting_db',
    'HOST': 'xxxxxx.database.windows.net',
    'PORT': '',
    'USER': 'xxxx@xxxxxx.database.windows.net',
    'PASSWORD': 'xxxxxx',

    'OPTIONS': {
        'driver': 'ODBC Driver 17 for SQL Server'
    }
}
soasuk commented 5 years ago

I found something similar with 2.1.0.0 when doing migrations. My migration had several steps, a later one failed and instead of rolling all steps back, the previous steps were still applied. An output of the SQL produced something like this:

BEGIN TRANSACTION **alter table steps COMMIT;

Running the same SQL directly in management studio produced the same failed step as via migrate, however all the previous steps were rolled back as expected.

sandipbgt commented 4 years ago

depending upon name of connections dictionary in settings.py try like this:

with transaction.atomic(using='azure_reporting'):
    MyModel.objects.filter(id=1).update(my_field='Test')
    time.sleep(20)