dumpdata的Django-pyodbc mssql错误

时间:2015-07-30 15:11:02

标签: django sql-server-2005 pyodbc freetds django-pyodbc

我正试图在我的django项目的mssql数据库上执行sudo ./manage.py dumpdata,但一直得到:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/commands/dumpdata.py", line 158, in handle
    stream=self.stdout)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 128, in serialize
    s.serialize(queryset, **options)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/serializers/base.py", line 52, in serialize
    for obj in queryset:
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/commands/dumpdata.py", line 150, in get_objects
    for obj in queryset.iterator():
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 701, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 785, in execute_sql
    cursor = self.connection.cursor()
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/backends/__init__.py", line 165, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/opt/bitnami/python/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 230, in _cursor
    autocommit=autocommit)
pyodbc.Error: ('IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found, and no default driver specified (0) (SQLDriverConnect)')

我完成了https://code.google.com/p/django-pyodbc/wiki/FreeTDS中提到的配置,我得到了所有测试工作(FreeTDS和ODBC)。

这是我当前的settings.py数据库配置:

DATABASES = {
    ...
    'vcoe':
        {
            'ENGINE' : 'sql_server.pyodbc',
            'NAME' : 'VOIP',
            'USER' : '******',
            'PASSWORD' : '******',
            'OPTIONS' : {
                'driver': 'FreeTDS',
                'dsn': 'VCOE',
            }
        }
}

DATABASE_ENGINE = 'sql_server.pyodbc'
DATABASE_NAME = 'VOIP'
DATABASE_USER = '******'
DATABASE_PASSWORD = '******'
DATABASE_OPTIONS = {
    'driver': 'FreeTDS',
    'dsn': 'VCOE', # ODBC DSN name defined in your odbc.ini
}

当我尝试运行./manage.py test时,我得到:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/test/runner.py", line 147, in run_tests
    old_config = self.setup_databases()
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/test/runner.py", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/test/runner.py", line 299, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/backends/creation.py", line 362, in create_test_db
    self._create_test_db(verbosity, autoclobber)
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/backends/creation.py", line 455, in _create_test_db
    with self._nodb_connection.cursor() as cursor:
  File "/opt/bitnami/python/lib/python2.7/site-packages/django/db/backends/__init__.py", line 167, in cursor
    cursor = utils.CursorWrapper(self._cursor(), self)
  File "/opt/bitnami/python/lib/python2.7/site-packages/sql_server/pyodbc/base.py", line 181, in _cursor
    raise ImproperlyConfigured('You need to specify NAME in your Django settings file.')
django.core.exceptions.ImproperlyConfigured: You need to specify NAME in your Django settings file.

我尝试删除DATABASES = {...}部分,只留下DATABASE_XXX,但获取:

django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

我目前在客户端上使用:

  • Ubuntu 14.04.1 LTS
  • Python 2.7.6
  • Django 1.7.8
  • pyodbc == 3.0.10
  • 的django-pyodbc == 0.2.8

服务器我到达的是:

  • Microsoft Server 2003正在运行:MSSQL 2005

以下是/etc/odbcinst.ini

[FreeTDS]
    Description = TDS driver (Sybase/MS SQL)
    Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
    Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
    CPTimeout =
    CPReuse =

双重检查路径,所有文件都存在

这是我的/etc/odbc.ini

[ODBC Data Sources]
ODBCNAME = Microsoft SQL Server

[VCOE]
Driver = FreeTDS
Description = VOIP Legacy Database
Trace = No
Servername = VOIP
Database = VOIP

[Default]
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so

这是我的/etc/freetds/freetds.conf

[VOIP]
host = 142.117.139.***
port = 1433
tds version = 8.0

1 个答案:

答案 0 :(得分:0)

您正在使用不再维护的非常非常旧的django-pyodbc版本。这是最后一次更新于2011年。我在使用dumpdatadbshell时遇到了旧版本的问题。我建议使用这个版本,积极维护,这在几个Django项目中对我来说非常有效:

https://github.com/michiya/django-pyodbc-azure

注意,要安装它,你必须这样做,因为你没有使用Django 1.8:

pip install django-pyodbc-azure<1.8

偏离主题:如果你刚刚开始一个项目,我建议你使用Django 1.8.x.虽然Django 1.7.x仍然受支持,但Django 1.8.x是一个长期版本,这意味着它将至少支持三年。祝你好运!