Django Admin登录失败:MSSQL提示auth_user对象不存在
Django Admin登录报错:MSSQL中找不到'auth_user'对象
错误信息
('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无效的对象名 'auth_user'。(208) (SQLExecDirectW); [42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无法准备语句。(8180)")
完整错误日志
内部服务器错误: /admin/login/ 回溯(最近的调用最后): 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", 第89行, 在 _execute中 return self.cursor.execute(sql, params) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\mssql\base.py", 第576行, 在 execute中 return self.cursor.execute(sql, params) pyodbc.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无效的对象名 'auth_user'。(208) (SQLExecDirectW); [42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无法准备语句。(8180)") 上述异常是以下异常的直接原因: 回溯(最近的调用最后): 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\asgiref\sync.py", 第472行, 在 thread_handler中 raise exc_info[1] 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", 第42行, 在 inner中 response = await get_response(request) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", 第253行, 在 _get_response_async中 response = await wrapped_callback( 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\asgiref\sync.py", 第435行, 在 __call__中 ret = await asyncio.wait_for(future, timeout=None) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\asyncio\tasks.py", 第408行, 在 wait_for中 return await fut 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\thread.py", 第58行, 在 run中 result = self.fn(*args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\asgiref\sync.py", 第476行, 在 thread_handler中 return func(*args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", 第46行, 在 _wrapper中 return bound_method(*args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\cache.py", 第62行, 在 _wrapped_view_func中 response = view_func(request, *args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\admin\sites.py", 第441行, 在 login中 return LoginView.as_view(**defaults)(request) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py", 第103行, 在 view中 return self.dispatch(request, *args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", 第46行, 在 _wrapper中 return bound_method(*args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\debug.py", 第92行, 在 sensitive_post_parameters_wrapper中 return view(request, *args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", 第46行, 在 _wrapper中 return bound_method(*args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", 第133行, 在 _wrapped_view中 response = view_func(request, *args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", 第46行, 在 _wrapper中 return bound_method(*args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\cache.py", 第62行, 在 _wrapped_view_func中 response = view_func(request, *args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\views.py", 第90行, 在 dispatch中 return super().dispatch(request, *args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py", 第142行, 在 dispatch中 return handler(request, *args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\edit.py", 第152行, 在 post中 if form.is_valid(): 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\forms\forms.py", 第205行, 在 is_valid中 return self.is_bound and not self.errors 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\forms\forms.py", 第200行, 在 errors中 self.full_clean() 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\forms\forms.py", 第438行, 在 full_clean中 self._clean_form() 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\forms\forms.py", 第459行, 在 _clean_form中 cleaned_data = self.clean() 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\forms.py", 第217行, 在 clean中 self.user_cache = authenticate( 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\decorators\debug.py", 第42行, 在 sensitive_variables_wrapper中 return func(*func_args, **func_kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\__init__.py", 第77行, 在 authenticate中 user = backend.authenticate(request, **credentials) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\backends.py", 第46行, 在 authenticate中 user = UserModel._default_manager.get_by_natural_key(username) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\base_user.py", 第46行, 在 get_by_natural_key中 return self.get(**{self.model.USERNAME_FIELD: username}) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\manager.py", 第85行, 在 manager_method中 return getattr(self.get_queryset(), name)(*args, **kwargs) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", 第646行, 在 get中 num = len(clone) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", 第376行, 在 __len__中 self._fetch_all() 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", 第1866行, 在 _fetch_all中 self._result_cache = list(self._iterable_class(self)) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", 第87行, 在 __iter__中 results = compiler.execute_sql( 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\compiler.py", 第1398行, 在 execute_sql中 cursor.execute(sql, params) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", 第103行, 在 execute中 return super().execute(sql, params) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", 第67行, 在 execute中 return self._execute_with_wrappers( 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", 第80行, 在 _execute_with_wrappers中 return executor(sql, params, many, context) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", 第84行, 在 _execute中 with self.db.wrap_database_errors: 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", 第91行, 在 __exit__中 raise dj_exc_value.with_traceback(traceback) from exc_value 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", 第89行, 在 _execute中 return self.cursor.execute(sql, params) 文件 "C:\Users\ALMA1210857\AppData\Local\Programs\Python\Python310\lib\site-packages\mssql\base.py", 第576行, 在 execute中 return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: ('42S02', "[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无效的对象名 'auth_user'。(208) (SQLExecDirectW); [42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无法准备语句。(8180)")
问题背景
此前已将auth_user表迁移至MSSQL数据库,且曾正常登录Django Admin,现在登录时触发上述错误。
排查方向与解决办法
- 检查数据库表是否存在:直接登录MSSQL数据库,确认目标库中存在
auth_user表,注意区分大小写(MSSQL默认可能区分表名大小写),同时验证Django连接的数据库是否为迁移时的目标库。 - 验证Schema配置:若
auth_user表在非默认Schema下,需在settings.py的数据库配置OPTIONS中指定Schema:'OPTIONS': { 'driver': 'ODBC Driver 17 for SQL Server', 'extra_params': 'Schema=dbo' # 替换为实际Schema名称 } - 重新执行迁移:迁移可能未完全执行,运行以下命令修复:
若自定义了用户模型,需确保python manage.py makemigrations auth python manage.py migrate authsettings.py中AUTH_USER_MODEL配置正确,且对应迁移已执行。 - 检查数据库权限:确认Django连接MSSQL使用的账号拥有
auth_user表的查询权限。 - 清除Django缓存:缓存的元数据可能过期,执行命令清除:
python manage.py clearcache
内容的提问来源于stack exchange,提问作者adi bahadur
相关产品推荐
相关产品推荐

