Collation Error When creating Django Database Models

1.4k views Asked by At

I have followed the various tutorials from philly and others to setup django with django-mssql-backend but have had no luck. I think the connection is working but when it is trying to parse the tables I get a collation error that it cannot get past. The specs of what I'm running are as follows:

  • django-mssql-backend: 2.8.1
  • django: 3.2
  • pyodbc: 4.0.30
    'server':{
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'database',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': 'hostname of server',
        'PORT': '',
        'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server',
            'unicode_results': True,
        },
    }

When I attempt to run the migration class creator or:

python manage.py inspectdb --database=server

I get the following error in the output:

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models
# Unable to inspect table 'ADObjectMemberships'
# The error was: __new__() missing 1 required positional argument: 'collation'
# Unable to inspect table 'ADObjects'
# The error was: __new__() missing 1 required positional argument: 'collation'

I am sure this is possible because I spooled up a different venv with django 1.8 installed and the old django-pyodbc-azure module installed and it connects to the tables and pulls their information. The biggest problem I have with it is that it stops with ~15 tables left in the DB and throws a memory error no matter what I do to fix it.

Any thoughts or help on the issue is greatly appreciated!

1

There are 1 answers

0
Griffen On

Confirmed through independent testing that the issue does appear to be a bug in version 3.2 and will put in a bug report. However I did come out with a workaround for now as follows:

  • Create new temporary virtual environment
  • Install django==3.0 pyodbc==4.0 django-mssql-backend==1.8
  • Create the database entry for the SQL Server in settings.py
  • run python manage.py inspectdb --database=yourentry > yourentry.py

Once you have created all the models for the existing database you want to use in your website you can grab each of the yourentry.py files and copy them to a submodel folder and import them into the main models.py file.

When the virtual environment is no longer needed it can be deleted.