syncdb - ValueError:解包的值太多

Posted

技术标签:

【中文标题】syncdb - ValueError:解包的值太多【英文标题】:syncdb - ValueError: too many values to unpack 【发布时间】:2013-11-11 05:07:46 【问题描述】:

关闭更新 - 我想我已经完成了它,这代表我有点愚蠢。

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

编辑添加我已经完成了全新的重新安装。擦除虚拟机并使其恢复活力。以下是已完成步骤的细分

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

下载 unixODBC

tar xzvf unixODBC-2.3.2.tar.gz

./配置

制作

制作安装

下载 FreeTDS

tar xzvf freetds-stable.tgz

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

制作

进行安装

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 用户 -P 密码有效

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

apt-get install python-setuptools

easy_install pip

pip 安装 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',
       ,
   

【问题讨论】:

你在 Django 中的模型定义是什么? 已更新以包含我制作的应用程序中的 models.py,我希望模型驻留在其中。 【参考方案1】:

这是版本不兼容的问题。

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

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

【讨论】:

以上是关于syncdb - ValueError:解包的值太多的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:解包的值太多(Python 2.7)

Makemigrations ValueError:解包的值太多

Tensorboard - ValueError:解包的值太多(预期 2)

异常类型:ValueError 异常值:解包的值太多 - Django

如何修复:ValueError:解包的值太多(预期为 2)PCA

ValueError:使用 Sage 绘图时“解包的值太多”