Introspection Bug - 在数据库级别而不是Schema Level

时间:2018-01-26 18:58:51

标签: python sql-server django python-3.x django-pyodbc-azure

更新6:在讨论IRC #django上的问题之后,我认为django-pyodbc-azure反省数据库并在数据库级别查找django_migrations,而不是模式水平。因此,它在数据库级别(来自原始应用程序)找到它,因此无法创建它。这可能是django-pyodbc-azure内省的一个错误。

更新5:我已经对模式进行了三次检查,除了名称之外它们是相同的。我还将django-pyodbc-azuredjango更新为最新的1.11版本。我很茫然。

更新4:我在新架构中添加了一个名为django_migrations的表,错误更改为invalid column name 'app'。所以看起来这些表最初应该在那里。但是,正如我原来的问题所提到的,这是不可能的,而不是过去的做法。

更新3:除原始项目的架构(已包含表格,包括django_migrations)外,没有其他架构可以正常工作。创建该模式后,在运行python manage.py runserver之类的内容之前,它没有任何表格。

更新2:架构差异是根本问题。我将原始用户的默认架构更改为我希望新应用程序使用的新默认架构,并且发生了同样的错误。为什么一个模式工作,另一个不工作?

更新:这似乎是不同用户的问题。两个用户都具有相同的SQL Server权限。每个用户都有一个为此目的创建的不同默认架构。原始项目的用户已在数据库中拥有表(包括django_migrations)。

原始

Python:3.6.4

django:1.11.9

django-pyodbc-azure:1.11.9

我有一个django项目(myProject)和一个django应用程序(myApp)。此项目在包含django-pyodbc-azure以连接到SQL Server的conda环境上运行。

默认设置所做的唯一更改如下:

  1. myApp.apps.MyAppConfig添加到INSTALLED_APPS
  2. DATABASES更改为:

    DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'HOST': 'hostname',
        'NAME': 'databasename',
        'USER': 'username',
        'PASSWORD': 'password',
        'OPTIONS': {
            'driver': 'SQL Server Native Client 11.0'
        },
    },
    }
    
  3. 当我尝试运行python manage.py runserver时,会发生以下错误:

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

    为什么会出现此问题,我该如何解决?它似乎在数据库中查找django_migrations,但尚未创建任何表。实际上,无法创建表格,因为python manage.py makemigrations会导致相同的错误。

    这是我建造的第二个django项目。第一个运行完美地运行相同的DATABASES设置(除了是另一个用户以获得不同的默认架构)和相同的环境。

1 个答案:

答案 0 :(得分:0)

您是否创建了数据库?如果我没记错,你需要手动完成。另请参阅此问题:https://github.com/michiya/django-pyodbc-azure/issues/103