Troubles of making Django Models work on MS-SQL on MS Azure?
Question: I want to create a django web on Azure.

I spent all day on trying to connect my model classes to an exist MSSQL
database on Azure.

It still did not work. My hand is tried.

I list my steps, and hope someone to help me thanks a lot!

Step1. Install dependency libraries

    
    
    sudo pip install --upgrade pip
    sudo pip install django-pyodbc
    sudo pip install django-sqlserver
    sudo pip install django-mssql
    sudo pip install django-pyodbc-azure
    brew install freetds
    brew install freetds --with-unixodbc
    

Step2. Configuration writing

    
    
    ~/.bash_profile
        #ODBC
        export ODBCSYSINI=/usr/local/opt/unixodbc/etc
        export ODBCINI=/usr/local/opt/unixodbc/etc/odbc.ini
    
    /etc/odbcinst.ini
        [FreeTDS]
        Driver=/usr/local/lib/libtdsodbc.so
        Setup=/usr/local/lib/libtdsodbc.so
        Server={host}
        UsageCount=1
        Port=1433
        Database={db name}
        User={user name}
        Password={password}
        TDS_Version=7.2
        client_charset=utf-8
    
    /etc/odbc.ini
        [FreeTDS]
        Driver = FreeTDS
        ServerName = {hostname}
        Database = {db name}
        UserName = {user name}
        Password = {password}
        Port = 1433
        Protocol = 7.2
        TDS_Version = 8.0
    

Step3. Try to connect DB.

    
    
    tsql -S FreeTDS -p 1433 -U {user name} -P {password}
    
    It’s ok to connect to ‘INFORMATION_SCHEMA’ DB。
    
    But when I try:
    
        tsql -S FreeTDS -p 1433 -U {user name} -P {password} -D {database name}
    
    I had problems:
    
        Msg 4075 (severity 16, state 1) from {hostname} Line 1:
            "The USE database statement failed because the database collation Chinese_Traditional_Stroke_Order_100_CS_AS_WS is not recognized by older client drivers. Try upgrading the client operating system or applying a service update to the database client software, or use a different collation. See SQL Server Books Online for more information on changing collations."
        Msg 18456 (severity 14, state 1) from {hostname} Line 1:
            "Login failed for user ‘{user name}’.”
        Error 20002 (severity 9):
            Adaptive Server connection failed
        There was a problem connecting to the server
    
    If I try “tsql -S FreeTDS -p 1433 -U {user name} -P {password}” and under,
    
    1> USE somedb
    2> go
        Msg 40508 (severity 16, state 1) from {hostname} Line 1:
            "USE statement is not supported to switch between databases. Use a new connection to connect to a different database."
    
    problems again. (I found some pages to says: USE is not work at a MSSQL DB on Azure)
    

Step4. Try: python manage.py inspectdb > models.py

    
    
    django.db.utils.Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
    

Step5. Try models

    
    
    settings.py
        DATABASES = {
            'default': {
                'ENGINE': 'sql_server.pyodbc',
                'NAME': ‘{database name}’,
                'USER': ‘{user name}’,
                'PASSWORD': ‘{password}’,
                'HOST': ‘{hostname}’,
                'PORT': '1433',
                'OPTIONS': {
                    'driver': 'FreeTDS',
                },
            }
        }
    
    and
    
    $python manage.py shell
    
    >>> from app.models import {ModelName}
    >>> {ModelName}.objects.all()
    
        Error: ('08001', '[08001] [unixODBC][FreeTDS][SQL Server]Unable to connect to data source (0) (SQLDriverConnect)')
    
    (again, no surprise.)
    

Environments:

  1. Python: 2.7.10
  2. Django: 1.9.4 (final)
  3. MS-SQL Server Version: V12
  4. OS: Mac OSX 10.11.3
  5. FreeTDS: 0.95.80
  6. Others
    
        pyobjc-core (2.5.1)
    pyobjc-framework-Accounts (2.5.1)
    pyobjc-framework-AddressBook (2.5.1)
    pyobjc-framework-AppleScriptKit (2.5.1)
    pyobjc-framework-AppleScriptObjC (2.5.1)
    pyobjc-framework-Automator (2.5.1)
    pyobjc-framework-CFNetwork (2.5.1)
    pyobjc-framework-Cocoa (2.5.1)
    pyobjc-framework-Collaboration (2.5.1)
    pyobjc-framework-CoreData (2.5.1)
    pyobjc-framework-CoreLocation (2.5.1)
    pyobjc-framework-CoreText (2.5.1)
    pyobjc-framework-DictionaryServices (2.5.1)
    pyobjc-framework-EventKit (2.5.1)
    pyobjc-framework-ExceptionHandling (2.5.1)
    pyobjc-framework-FSEvents (2.5.1)
    pyobjc-framework-InputMethodKit (2.5.1)
    pyobjc-framework-InstallerPlugins (2.5.1)
    pyobjc-framework-InstantMessage (2.5.1)
    pyobjc-framework-LatentSemanticMapping (2.5.1)
    pyobjc-framework-LaunchServices (2.5.1)
    pyobjc-framework-Message (2.5.1)
    pyobjc-framework-OpenDirectory (2.5.1)
    pyobjc-framework-PreferencePanes (2.5.1)
    pyobjc-framework-PubSub (2.5.1)
    pyobjc-framework-QTKit (2.5.1)
    pyobjc-framework-Quartz (2.5.1)
    pyobjc-framework-ScreenSaver (2.5.1)
    pyobjc-framework-ScriptingBridge (2.5.1)
    pyobjc-framework-SearchKit (2.5.1)
    pyobjc-framework-ServiceManagement (2.5.1)
    pyobjc-framework-Social (2.5.1)
    pyobjc-framework-SyncServices (2.5.1)
    pyobjc-framework-SystemConfiguration (2.5.1)
    pyobjc-framework-WebKit (2.5.1)
    pyodbc (3.0.10)
    django-mssql (1.6.2)
    django-pyodbc-azure (1.9.3.0)
    django-sqlserver (1.7)
    


Answer: Per my experience, it seems that you need to configure the Driver
`/usr/local/lib/libtdsodbc.so` of FreeTDS in the `odbc.ini` file on your Mac
OS.

There is [a similiar answered
thread](http://stackoverflow.com/questions/29571568/how-to-connect-to-azure-
sql-database-from-django-app-on-linux-vm) you can refer to that suggest to use
`pymssql` package to connect Azure SQL Database. Also, you can see the Azure
offical doc [Connect to SQL Database by using Python on Ubuntu
Linux](https://azure.microsoft.com/en-us/documentation/articles/sql-database-
develop-python-simple-ubuntu-linux/).

