0
votes

Am getting an error in making migrations for my current django project.

below is the code written in settings.py file to connect to MSSQL Server.

DATABASES = {
'default': {
    'ENGINE':'sql_server.pyodbc',
    'NAME':'JTPROD',
    'HOST':'TZACL5X8H1N2\SQLEXPRESS', ##this is my local machine database
    'USER':'xxx', ##ommitted for the post 
    'PASSWORD':'xxx',##ommitted for tht post
    'PORT':'',

    'OPTIONS':{
        'provider': 'SQLOLEDB', # Have also tried 'SQLCLI11' and 'SQLCLI10'
        'extra_params': 'DataTypeCompatibility=80',
        'driver':'SQL Server',
             },
    },
}

but am getting the following error when I run "python manage.py migrate

Traceback (most recent call last): File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection self.connect() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 194, in connect self.connection = self.get_new_connection(conn_params) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 307, in get_new_connection timeout=timeout) pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "manage.py", line 15, in execute_from_command_line(sys.argv) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management__init__.py", line 381, in execute_from_command_line utility.execute() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute output = self.handle(*args, **options) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 82, in handle executor = MigrationExecutor(connection, self.migration_progress_callback) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 18, in init self.loader = MigrationLoader(self.connection) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\loader.py", line 49, in init self.build_graph() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph self.applied_migrations = recorder.applied_migrations() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\recorder.py", line 61, in applied_migrations if self.has_table(): File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\recorder.py", line 44, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 255, in cursor return self._cursor() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 232, in _cursor self.ensure_connection() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection self.connect() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection self.connect() File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\base.py", line 194, in connect self.connection = self.get_new_connection(conn_params) File "C:\Users\elukamis\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sql_server\pyodbc\base.py", line 307, in get_new_connection timeout=timeout) django.db.utils.OperationalError: ('08001', '[08001] [Microsoft][ODBC SQL Server Driver]Neither DSN nor SERVER keyword supplied (0) (SQLDriverConnect); [08001] [Microsoft][ODBC SQL Server Driver]Invalid connection string attribute (0)')

Does anyone have an idea on what I might be missing in my configuration ?

1
Your 'HOST' key contains a backslash: 'TZACL5X8H1N2\SQLEXPRESS', what if you use a raw string literal? r'TZACL5X8H1N2\SQLEXPRESS'.Willem Van Onsem
Thanks, just tried your suggestion but am getting the same erroreddykakaa
The backslash I have put is for 'server\instance'eddykakaa
server\instance is for explicitly named (intentionally chosen as "named instance" type of installation) instances only. default instance cannot be accessed in this way. Try removing \SQLEXPRESS.Ivan Starostin
Still getting the same error even after removing \SQLEXPRESS.eddykakaa

1 Answers

0
votes

If you're still looking for an answer, I'm not sure this will help you, but it helped me when I was getting a similar error. SQL Server Express has TCP/IP connections disabled by default, but they apparently need to be enabled in order to allow a connection like this.

Open SQL Server Configuration Manager, expand 'SQL Server Network Configuration', and click on 'Protocols for SQLEXPRESS'. Assuming the entry for TCP/IP shows as Disabled, right-click that and select Properties. On the 'Protocol' tab, set Enabled to Yes. I also updated the 'IP Addresses' tab by entering '1433' into the TCP Port field of the 'IPAll' section (at the bottom). Then I specified 'PORT':'1433', in the settings.py DATABASES configuration section. I'm not sure that was necessary, but following those steps allowed my app to successfully connect to SQL Server Express.