尝试运行服务器时对象名称“django_migrations”无效

4si2a6ki  于 2022-11-18  发布在  Go
关注(0)|答案(2)|浏览(182)

我想把我的Django应用连接到客户端的MSSQL数据库(以前我的应用在SQLite上工作)。我在他们的测试服务器上做了一个迁移,它成功地工作了,然后他们把这个数据库复制到目标服务器上,当我试图

python manage.py runserver

它让我看到

django.db.utils.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'django_migrations'. (208) (SQLExecDirectW)")

这可能有什么问题?这是我的连接:

DATABASES = {
   'default': {
       'ENGINE': 'sql_server.pyodbc',
       'NAME': 'ESD',
       'HOST': 'IPaddress',
       'USER': 'Username',
       'PASSWORD': 'Password',

       'OPTIONS': {
            'driver': 'ODBC Driver 17 for SQL Server'
        }
   }
}

我也尝试迁移,但显示相同的错误
出现所有错误:

(norm)esd@server:~/Desktop/norm/myproject> python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
Unhandled exception in thread started by <function wrapper at 0x7fc57eb4f8c0>
Traceback (most recent call last):
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check_migrations()
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/core/management/commands/runserver.py", line 163, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/migrations/loader.py", line 176, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/home/esd/Desktop/norm/lib64/python2.7/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 "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
    self._fetch_all()
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/models/query.py", line 128, in __iter__
    for row in compiler.results_iter():
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/models/sql/compiler.py", line 802, in results_iter
    results = self.execute_sql(MULTI)
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
    cursor.execute(sql, params)
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/esd/Desktop/norm/lib64/python2.7/site-packages/sql_server/pyodbc/base.py", line 537, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'django_migrations'. (208) (SQLExecDirectW)")
5tmbdcev

5tmbdcev1#

所以发生这种情况的原因很可能是因为你为django的内部表设置的默认模式和你当前用户的默认模式不一样。
您可以通过检查数据库中django_migrations表的实际模式来对此进行测试,然后运行python脚本来验证当前的默认模式,如下所示:

import sqlalchemy as db
from sqlalchemy.sql import text

db_host = "Your host URL"
db_name = "Your DB name"

engine = db.create_engine(
    f"mssql+pyodbc://@{db_host}/{db_name}?trusted_connection=yes&driver=SQL+Server+Native+Client+11.0"
)

statement = text("SELECT SCHEMA_NAME();")

with engine.connect() as connection:
    with connection.begin():
        results = connection.execute(statement)

for result in results:
    print(result)

如果它们不相同,请进入SQL Server管理器,将用户的默认架构设置为正确的架构。
要查看您以哪个用户身份连接,只需将SELECT SCHEMA_NAME()替换为SELECT SYSTEM_USER,然后再次运行

lf5gs5x2

lf5gs5x22#

也许您需要先运行"py manage.py makemigrations",然后再运行"py manage.py migrate".
如果没有,也许值得为数据库添加特定的主机和端口。

> DATABASES = {
>     'default': {
>         'ENGINE': 'django.db.backends.mysql',
>         'NAME': 'pkg',
>         'HOST': '127.0.0.1',
>         'PORT': '3306',
>         'USER': 'root',
>         'PASSWD': '',
>     } }

不确定它是否有效,但也许值得一试。

相关问题