我的Django应用通常可以在SQLite3上运行,但是这次我不得不将其转换为使用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)")
尝试迁移,运行服务器等时出现错误。
我发现当我直接从Linux(sqlcmd)登录数据库时,我也无法执行“正常”选择,例如
SELECT * FROM django_migrations;
今天我发现我可以这样使用它:
SELECT * FROM db_owner.django_migrations;
这将结束我的问题,但是如何告诉Django模型以这种方式使用它或修复数据库?在测试数据库(其他服务器,相同的SQL Serverversion等)上,一切都很好。当我尝试从没有db_owner前缀的表中进行选择时,出现此错误:
django.db.utils.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'django_migrations'. (208) (SQLExecDirectW)")
答案 0 :(得分:0)
正如Ivan所说,我更改了所有表以使用dbo模式而不是db_owner,一切开始正常工作!我做到了
ALTER SCHEMA dbo TRANSFER db_owner.tableName;
在数据库中的每个表上。