1

更新 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)。

原来的:

蟒蛇:3.6.4

django:1.11.9

django-pyodbc-天蓝色:1.11.9

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

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

  1. 添加myApp.apps.MyAppConfigINSTALLED_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'
        },
    },
    }
    

当我尝试运行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设置(除了作为不同的用户获得不同的默认模式)和相同的环境完美运行。

4

1 回答 1

0

你创建数据库了吗?如果我没记错的话,您需要手动执行此操作。另请参阅此问题:https ://github.com/michiya/django-pyodbc-azure/issues/103

于 2018-01-26T19:14:29.567 回答