syncdb - ValueError:要解压缩的值太多

时间:2013-11-11 05:07:47

标签: django sql-server-2008 django-pyodbc

关闭更新 - 我想我会通过它,代表我有点愚蠢。

python manage.py inspectdb> models.py

我忘记将输出传递给文件了!

我已经将它转移到我的app / models.py中,现在我只是试图让它从那里运行但是更进一步。

我正在尝试让django针对SQL Server 2008 R2服务器运行。

我已经找到了FreeTDS / unixODBC连接。我可以成功地对数据库进行isql和tsql查询。

我已经能够直接在python中建立连接 -

import pyodbc
cnxn = pyodbc.connect('DRIVER=FreeTDS;SERVER=servername;DATABASE=dbname;UID=user;PWD=pass;TDS_Version=8.0;')
cursor = cnxn.cursor()
result = cursor.execute("select name from tablea where name = 'hello'")
print result

如果我在django shell中执行了inspectdb,那很好。它返回70多个表格,它们的布局完全正常。

一旦我尝试做一个syndb,它就会溢出以下错误 -

Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/syncdb.py", line 112, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/sql.py", line 216, in emit_post_sync_signal
    interactive=interactive, db=db)
  File "/usr/local/lib/python2.6/dist-packages/django/dispatch/dispatcher.py", line 185, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/management/__init__.py", line 82, in create_permissions
    ctype = ContentType.objects.db_manager(db).get_for_model(klass)
  File "/usr/local/lib/python2.6/dist-packages/django/contrib/contenttypes/models.py", line 47, in get_for_model
    defaults = {'name': smart_text(opts.verbose_name_raw)},
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 154, in get_or_create
    return self.get_queryset().get_or_create(**kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 373, in get_or_create
    return self.get(**lookup), False
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 301, in get
    num = len(clone)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 77, in __len__
    self._fetch_all()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 710, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 771, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 48, in as_sql
    self._get_ordering(out_cols, supports_offset_clause or not do_offset)
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 154, in _get_ordering
    ordering, ordering_group_by = self.get_ordering()
ValueError: too many values to unpack

类似地,如果我尝试在django中进行查询,就像这样 -

from myapp.models import tablea
tablea.objects.filter(id=5)

失败并显示以下错误消息 -

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 71, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 710, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 771, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 48, in as_sql
    self._get_ordering(out_cols, supports_offset_clause or not do_offset)
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 154, in _get_ordering
    ordering, ordering_group_by = self.get_ordering()
ValueError: too many values to unpack

我不相信这些错误与此处发现的其他线程有关 -

ValueError: Too many values to unpack Django

但我不是专家,所以我错了。我知道我确实尝试更改密码,它不会让我连接类似的错误消息 -

>>> u = User.objects.get(username="user")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 151, in get
    return self.get_queryset().get(*args, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 301, in get
    num = len(clone)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 77, in __len__
    self._fetch_all()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 710, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 771, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 48, in as_sql
    self._get_ordering(out_cols, supports_offset_clause or not do_offset)
  File "/usr/local/lib/python2.6/dist-packages/sql_server/pyodbc/compiler.py", line 154, in _get_ordering
    ordering, ordering_group_by = self.get_ordering()
ValueError: too many values to unpack

编辑添加 - 在进行查询时,我的模型定义如下 -

from django.db import models

# Create your models here.

class tablea(models.Model):
    tablea_id = models.IntegerField()
    tablea_name = models.CharField(max_length=32)
    tablea_duration = models.IntegerField()
class Meta:
    db_table = u'tablea'
    def __unicode__(self):
        return self.tablea_name, tablea_duration

编辑添加我已完成全新的重新安装。擦拭VM并使其恢复生机。以下是已完成步骤的细分

apt-get install gcc g ++ make python-dev

下载unixODBC

tar xzvf unixODBC-2.3.2.tar.gz

./配置

makeinstall

下载FreeTDS

tar xzvf freetds-stable.tgz

./ configure --with-tdsver = 8.0 --with-unixodbc = usr / local

make install

ldconfig -v

ODBC.INI

[mssql]
Description             = MSSQL Server
Driver                  = freetds
Database                = database
Server              = server
Port            = 1433
TDS_Version             = 8.0

ODBCINST.INI

[freetds]
Description     = MS SQL database access with Free TDS
Driver          = /usr/local/lib/libtdsodbc.so
Setup           = /usr/local/lib/libtdsS.so
UsageCount      = 1

freetds.conf

[mssql]
        host = server
        port = 1433
        tds version = 8.0

tsql -S mssql -U user -P password works

isql -v mssql用户密码有效(出于某种原因简单的isql -v mssql没有)

apt-get install python-setuptools

easy_install pip

pip install pyodbc

pip install Django == 1.6

pip install django-pyodbc-azure

settings.py

DATABASES = {
   'default': {
       'ENGINE': 'sql_server.pyodbc',
       'HOST': "server\instance",
       'USER': "username",
       'PASSWORD': "password",
       'NAME': "database",
       'OPTIONS': {
           'host_is_server': True,
           'driver': 'FreeTDS',
           'dsn': 'MSSQL',
       },
   }
}

1 个答案:

答案 0 :(得分:3)

这是版本不兼容问题。

Django 1.6改变了django.db.models.sql.compiler.SQLCompiler.get_ordering返回的值类型。它返回 3元素元组,但pyodbc仅解包两个元素。

如果您查看Django 1.5 version,您会发现它曾经像pyodbc所期望的那样返回 2元素元组