When i run python manage.py makemigrations
it throws an error:
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
I discovered the problem was in one of my 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":
...
If i comment out the lines marked with comment error and then run the migrations, it works perfectly fine. But this workaround is not suitable for deploying into production is there a way to fix this?
CodePudding user response:
These two lines from your error stacktrace show that you are importing views into your 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')
And in your views.py you are importing the Group model. This is a common import order issue for django apps. What happens here is when your app is started, it goes through your urls.py
first before your models are loaded, and because of the import statements you have you are loading the models before they are registered.
Instead of having these lines on views.py
, try move them into the functions where you need them. That should resolve the issue.
# create groups
compute, created = Group.objects.get_or_create(name='compute') # Error
search, created = Group.objects.get_or_create(name='search') # error