使用先前工作配置的django-pyodbc-azure回滚错误 - 第389行

时间:2015-11-12 14:13:38

标签: python sql-server django pyodbc django-pyodbc-azure

我已经在Linux上使用django-pyodbc-azure一段时间了,还有pydobc,FreeTDS和unixODBC将Django连接到SQL Server 2014.我遇到了这个问题,我的应用程序运行正常,我正在调试它。为了重现这个问题,我开始使用全新的Django应用程序来保持简单。这是我的生活:

(azuretest)[vagrant@vagrant azuretest]$ pip freeze
Django==1.8.6
django-pyodbc-azure==1.8.3.0
pyodbc==3.0.10

这是我的数据库配置连接到SQL Server:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'HOST': 'myserver.com',
        'PORT': '1433',
        'NAME': 'my_db',
        'USER': 'my_db_user',
        'PASSWORD': 'mypw',
        'AUTOCOMMIT': True,
        'OPTIONS': {
            'driver': 'FreeTDS',
            'autocommit': True,
            'unicode_results': True,
            'host_is_server': True,
            'extra_params': 'tds_version=7.2',
        },
    },
}

我创建了一个简单的models.py:

class TestTemp(models.Model):
    tempdate = models.DateField()

这个设置在一个相当复杂的Django项目中运行良好,该项目仍可以SELECT到同一个数据库。但是,每当我尝试进行更新或迁移时,我都会收到此错误:

(azuretest)[vagrant@vagrant azuretest]$ ./manage.py migrate home
Operations to perform:
  Apply all migrations: home
Running migrations:
  Rendering model states... DONE
  Applying home.0001_initial...Traceback (most recent call last):
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/sql_server/pyodbc/base.py", line 389, in _set_aut
ocommit
    self.connection.rollback()
pyodbc.Error: ('HY000', 'The driver did not supply an error!')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in
 execute_from_command_line
    utility.execute()
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/core/management/__init__.py", line 346, in
 execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/core/management/base.py", line 394, in run
_from_argv
    self.execute(*args, **cmd_options)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/core/management/base.py", line 445, in exe
cute
    output = self.handle(*args, **options)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line
 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/db/migrations/executor.py", line 110, in m
igrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/db/migrations/executor.py", line 154, in a
pply_migration
    self.recorder.record_applied(migration.app_label, migration.name)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/db/migrations/recorder.py", line 67, in re
cord_applied
    self.migration_qs.create(app=app, name=name)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/db/models/query.py", line 348, in create
    obj.save(force_insert=True, using=self.db)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/db/models/base.py", line 734, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/db/models/base.py", line 759, in save_base

    with transaction.atomic(using=using, savepoint=False):
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/db/transaction.py", line 186, in __enter__

    connection.set_autocommit(False)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/db/backends/base/base.py", line 295, in se
t_autocommit
    self._set_autocommit(autocommit)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/sql_server/pyodbc/base.py", line 390, in _set_aut
ocommit
    self.connection.autocommit = autocommit
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/db/utils.py", line 98, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/home/vagrant/.virtualenvs/azuretest/lib/python3.4/site-packages/sql_server/pyodbc/base.py", line 389, in _set_aut
ocommit
    self.connection.rollback()
django.db.utils.Error: ('HY000', 'The driver did not supply an error!')

奇怪的是它在SQL Server([home_testtemp])中成功创建了表,并且似乎在不必要的回滚上出错了。有关进一步调试或解决问题的最佳方法的任何想法?当我通过./manage.py sqlmigrate home直接对使用此用户名和密码登录的数据库运行SQL输出时,它可以正常工作。

提前致谢。

更新1:这不是一个很好的解决方案,但是当它不应该被调用时,可以解决看起来有问题的回滚问题。这个变化发生在django-pyodbc-azure的base.py第389行:

这是丑陋的,摆脱了保障,但同时使事情有效。修改django-pyodbc-azure中第389行的base.py:

def _set_autocommit(self, autocommit):
        self.connection.commit()

#        with self.wrap_database_errors:
#            if autocommit:
#                self.connection.commit()
#            else:
#                self.connection.rollback()
#            self.connection.autocommit = autocommit

显然,仍然在寻找一个实际的修复而不是黑客和根本原因。

更新2:将上面更新1中所做的更改退回到原始django-pyodbc。然后在设置中,将tds_version更改为7.07.1。有用。如果您将其更改为7.27.3,则会中断。这可能是从SQL Server 2008开始可用的新DATE字段的问题吗?无论哪种方式,临时解决方案都是恢复到TDS版本7.1,这显然是有用的修复信息。

2 个答案:

答案 0 :(得分:3)

已发布新版本的django-pyodbc-azure,为我解决了这个问题(谢谢Michaya)。要修复它,只需升级:

pip install django-pyodbc-azure==1.8.6

...并在必要时更新您的需求文件。详情请见https://github.com/michiya/django-pyodbc-azure/issues/46

答案 1 :(得分:2)

因为它适用于7.0和7.1,我想知道您的SQL Server 2014数据库是否设置为SQL Server 2000兼容级别,这将限制您使用TDS 7.1或更低版本。

链接:

TDS Versions

Product Behavior