1

Update 6: After discussing the issue on the IRC #django, I think django-pyodbc-azure is introspecting the database and looking for django_migrations at the database level, not the schema level. Therefore, it's finding it at the database level (from the original app), and therefore cannot create it. This might be a bug in the introspection of django-pyodbc-azure.

Update 5: I have triple checked the schemas, and they are identical besides the name. I also updated django-pyodbc-azure and django to the latest 1.11 versions. I am at a loss.

Update 4: I added a table called django_migrations to the new schema, and the error changed to invalid column name 'app'. So it seems like the tables are expected to be there initially. However, as mentioned in my original question, this is impossible and not how it has been done in the past.

Update 3: No other schema works except the schema for the original project (which already has tables, including django_migrations). When that schema was created, it did not have any tables in it prior to running things like python manage.py runserver.

UPDATE 2: The schema difference is the root issue. I changed the default schema of the original user to the new default schema I want the new app to use, and the same error occurred. Why is one schema working, and the other is not?

UPDATE: It seems to be a problem with the different users. Both users have the same SQL Server permissions. Each user has a different default schema that was created for this purpose. The user for the original project has tables (including django_migrations) already in the database.

Original:

Python: 3.6.4

django: 1.11.9

django-pyodbc-azure: 1.11.9

I have a django project (myProject) with one django app (myApp). This project is run on a conda environment that includes django-pyodbc-azure to connect to SQL Server.

The only changes made from the default setup are the following:

  1. Added myApp.apps.MyAppConfig to INSTALLED_APPS
  2. Changed DATABASES to this:

    DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'HOST': 'hostname',
        'NAME': 'databasename',
        'USER': 'username',
        'PASSWORD': 'password',
        'OPTIONS': {
            'driver': 'SQL Server Native Client 11.0'
        },
    },
    }
    

When I attempt to run python manage.py runserver, the following error occurs:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\pathtodjangoproject\myProject>activate myApp

(myApp) C:\pathtodjangoproject\myProject>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
Unhandled exception in thread started by <function check_errors.<locals>.wrapper
 at 0x000000000422A378>
Traceback (most recent call last):
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\sql_serve
r\pyodbc\base.py", line 545, in execute
    return self.cursor.execute(sql, params)
pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][SQL Server Native Client
 11.0][SQL Server]Invalid object name 'django_migrations'. (208) (SQLExecDirectW
)")

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

Traceback (most recent call last):
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\ut
ils\autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\co
re\management\commands\runserver.py", line 128, in inner_run
    self.check_migrations()
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\co
re\management\base.py", line 422, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\migrations\executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\migrations\loader.py", line 52, in __init__
    self.build_graph()
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\migrations\loader.py", line 209, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\migrations\recorder.py", line 66, in applied_migrations
    return set(tuple(x) for x in self.migration_qs.values_list("app", "name"))
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\models\query.py", line 250, in __iter__
    self._fetch_all()
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\models\query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\models\query.py", line 122, in __iter__
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch):
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\models\sql\compiler.py", line 841, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\models\sql\compiler.py", line 899, in execute_sql
    raise original_exception
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\models\sql\compiler.py", line 889, in execute_sql
    cursor.execute(sql, params)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\backends\utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\ut
ils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\django\db
\backends\utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "C:\pathtoanaconda\anaconda\envs\myApp\lib\site-packages\sql_serve
r\pyodbc\base.py", line 545, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42S02', "[42S02] [Microsoft][SQL Server Nati
ve Client 11.0][SQL Server]Invalid object name 'django_migrations'. (208) (SQLEx
ecDirectW)")

Why is this issue occurring, and how do I fix it? It seems like it is looking for django_migrations in the database, but no tables have been created yet. In fact, tables can't be created because python manage.py makemigrations causes the same error.

This is the second django project I've built. The first runs flawlessly with the same DATABASES settings (besides being a different user to get a different default schema) and the same environment.

OverflowingTheGlass
  • 2,324
  • 1
  • 27
  • 75

1 Answers1

0

Did you create the database ? If I recall correctly you need to do that manually. Also see this issue: https://github.com/michiya/django-pyodbc-azure/issues/103

  • the database already exists, yes. it is our main database, i just have a default schema set for the username. this is the same way i handled the previous project without issue. – OverflowingTheGlass Jan 26 '18 at 19:16