Django 错误 no such table: auth_group when 运行 makemigrations
Django error no such table: auth_group when running makemigrations
当我运行python manage.py makemigrations
它抛出一个错误:
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/smoke/Documents/wsl_dev/testing/genelookup/apps/authentication/urls.py", line 7, in <module>
from .views import login_view, register_user
File "/home/smoke/Documents/wsl_dev/testing/genelookup/apps/authentication/views.py", line 14, in <module>
compute, created = Group.objects.get_or_create(name='compute')
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 588, in get_or_create
return self.get(**kwargs), False
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 435, in get
num = len(clone)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 262, in __len__
self._fetch_all()
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 1354, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1202, in execute_sql
cursor.execute(sql, params)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 416, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: auth_group
我发现问题出在我的 views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from .forms import LoginForm, SignUpForm
from django.contrib.auth.models import Group # error
from apps.utils import unauthenticated_user
# create groups
compute, created = Group.objects.get_or_create(name='compute') # Error
search, created = Group.objects.get_or_create(name='search') # error
@unauthenticated_user
def login_view(request):
form = LoginForm(request.POST or None)
msg = None
if request.method == "POST":
...
如果我注释掉标有注释错误的行,然后 运行 迁移,它工作得很好。但此解决方法不适合部署到生产中,有没有办法解决这个问题?
错误堆栈跟踪中的这两行表明您正在将视图导入 urls.py
File "/home/smoke/Documents/wsl_dev/testing/genelookup/apps/authentication/urls.py", line 7, in <module>
from .views import login_view, register_user
File "/home/smoke/Documents/wsl_dev/testing/genelookup/apps/authentication/views.py", line 14, in <module>
compute, created = Group.objects.get_or_create(name='compute')
并且在您的 views.py 中您正在导入组模型。这是 django 应用程序常见的导入顺序问题。这里发生的事情是当你的应用程序启动时,它会在你的模型加载之前首先通过你的urls.py
,并且由于你有导入语句,你在模型注册之前加载它们。
不要将这些行放在 views.py
上,而是尝试将它们移动到您需要它们的函数中。这应该可以解决问题。
# create groups
compute, created = Group.objects.get_or_create(name='compute') # Error
search, created = Group.objects.get_or_create(name='search') # error
当我运行python manage.py makemigrations
它抛出一个错误:
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/smoke/Documents/wsl_dev/testing/genelookup/apps/authentication/urls.py", line 7, in <module>
from .views import login_view, register_user
File "/home/smoke/Documents/wsl_dev/testing/genelookup/apps/authentication/views.py", line 14, in <module>
compute, created = Group.objects.get_or_create(name='compute')
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 588, in get_or_create
return self.get(**kwargs), False
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 435, in get
num = len(clone)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 262, in __len__
self._fetch_all()
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 1354, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1202, in execute_sql
cursor.execute(sql, params)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/smoke/miniconda3/envs/testing/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 416, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: auth_group
我发现问题出在我的 views.py
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from .forms import LoginForm, SignUpForm
from django.contrib.auth.models import Group # error
from apps.utils import unauthenticated_user
# create groups
compute, created = Group.objects.get_or_create(name='compute') # Error
search, created = Group.objects.get_or_create(name='search') # error
@unauthenticated_user
def login_view(request):
form = LoginForm(request.POST or None)
msg = None
if request.method == "POST":
...
如果我注释掉标有注释错误的行,然后 运行 迁移,它工作得很好。但此解决方法不适合部署到生产中,有没有办法解决这个问题?
错误堆栈跟踪中的这两行表明您正在将视图导入 urls.py
File "/home/smoke/Documents/wsl_dev/testing/genelookup/apps/authentication/urls.py", line 7, in <module>
from .views import login_view, register_user
File "/home/smoke/Documents/wsl_dev/testing/genelookup/apps/authentication/views.py", line 14, in <module>
compute, created = Group.objects.get_or_create(name='compute')
并且在您的 views.py 中您正在导入组模型。这是 django 应用程序常见的导入顺序问题。这里发生的事情是当你的应用程序启动时,它会在你的模型加载之前首先通过你的urls.py
,并且由于你有导入语句,你在模型注册之前加载它们。
不要将这些行放在 views.py
上,而是尝试将它们移动到您需要它们的函数中。这应该可以解决问题。
# create groups
compute, created = Group.objects.get_or_create(name='compute') # Error
search, created = Group.objects.get_or_create(name='search') # error