1

UPDATE for close - I think I go through it and it was a bit of stupidity on my behalf.

python manage.py inspectdb > models.py

I was forgetting to pipe the output to a file!

I've dumped that into my app/models.py and now I'm just trying to get it running from there but going further.

I'm trying to get django running against an SQL Server 2008 R2 server.

I've figured out the FreeTDS/unixODBC connections. I can successfully do an isql and tsql query against the database.

I've been able to do a connection directly in python like so -

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

If I do an inspectdb in django shell it's fine. It returns 70+ tables and their layout perfectly ok.

once I try to do a syndb it's spilling back the following error -

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

similarily if I try to do a query in django like so -

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

It fails with the following error message -

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

I'm not convinced these errors are related to the other thread found here -

ValueError: Too many values to unpack Django

But I'm no expert so I could be wrong. I know I did try to change my password and it won't let me connect similar error message -

>>> 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

edit to add - when doing a query my model definition is as follows -

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

edit to add that I've done a complete fresh reinstall. Wiped the VM and brought it back to life. Here's a breakdown of the steps done

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

download unixODBC

tar xzvf unixODBC-2.3.2.tar.gz

./configure

make

makeinstall

download FreeTDS

tar xzvf freetds-stable.tgz

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

make

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 user password works (for some reason simple isql -v mssql does not)

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',
       },
   }
}
Community
  • 1
  • 1
whoisearth
  • 4,080
  • 13
  • 62
  • 130

1 Answers1

3

It's a version incompatibility issue.

Django 1.6 changed the kind of value returned by django.db.models.sql.compiler.SQLCompiler.get_ordering. It returns a 3-element tuple, but pyodbc unpacks just two elements.

If you look at Django 1.5 version you see that it used to return a 2-element tuple as pyodbc expects.

Nadir Sampaoli
  • 5,437
  • 4
  • 22
  • 32