Django Migration not starting on ubuntu - django

i have uploaded my Django project to an ubuntu server. and deleted all migration files on project folder. I was using mysql on my localhost, but installed Postgresql on Ubuntu. When i try to migrate database django raises errors like below.
python manage.py migrate results in
F File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/root/Virtual/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/root/Virtual/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/root/Virtual/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/root/Virtual/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 60, in execute
super().execute(*args, **options)
File "/root/Virtual/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/root/Virtual/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 95, in handle
self.run(**options)
File "/root/Virtual/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 102, in run
autoreload.run_with_reloader(self.inner_run, **options)
File "/root/Virtual/lib/python3.7/site-packages/django/utils/autoreload.py", line 579, in run_with_reloader
start_django(reloader, main_func, *args, **kwargs)
File "/root/Virtual/lib/python3.7/site-packages/django/utils/autoreload.py", line 564, in start_django
reloader.run(django_main_thread)
File "/root/Virtual/lib/python3.7/site-packages/django/utils/autoreload.py", line 272, in run
get_resolver().urlconf_module
File "/root/Virtual/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/root/Virtual/lib/python3.7/site-packages/django/urls/resolvers.py", line 564, in urlconf_module
return import_module(self.urlconf_name)
File "/root/Virtual/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/root/Virtual/AdenOnline/AdenOnline/urls.py", line 13, in <module>
path('taskmanager/',include('taskmanager.urls') ),
File "/root/Virtual/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/root/Virtual/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/root/Virtual/AdenOnline/taskmanager/urls.py", line 6, in <module>
from .views import MainView,TaskDetailView,TaskListView
File "/root/Virtual/AdenOnline/taskmanager/views.py", line 107, in <module>
class TaskListView(ListView):
File "/root/Virtual/AdenOnline/taskmanager/views.py", line 110, in TaskListView
queryset = list(Task.objects.all().values('id','task_project','task_title','task_employee','task_employer','task_start_date','task_end_date','task_status','task_priorty'))
File "/root/Virtual/lib/python3.7/site-packages/django/db/models/query.py", line 274, in __iter__
self._fetch_all()
File "/root/Virtual/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/root/Virtual/lib/python3.7/site-packages/django/db/models/query.py", line 113, in __iter__
for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
File "/root/Virtual/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1049, in results_iter
results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
File "/root/Virtual/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1097, in execute_sql
cursor.execute(sql, params)
File "/root/Virtual/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/root/Virtual/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/root/Virtual/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/root/Virtual/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/root/Virtual/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/root/Virtual/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "taskmanager_task" does not exist
LINE 1: ...k_status", "taskmanager_task"."task_priorty" FROM "taskmanag...
Does this because of i switched PostgreSQL on server or Django ? Any ideas and suggestions will be appreciated. Thank a lot.

Related

django db ProgrammingError

When i try to reset database (i created a new empty database)
i got this error when i run this command python manage.py makemigrations (or migrate)
for pattern in self.url_patterns:
File "django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File \python\python39\lib\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 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "project\urls.py", line 18, in <module>
path('api/v2/', include('project.api.urls'), name='api'),
File "django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "python\python39\lib\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 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "project\api\urls.py", line 3, in <module>
from .views import *
File "project\api\views.py", line 146, in <module>
class SampleViewSet(viewsets.ModelViewSet):
File "project\api\views.py", line 148, in SampleViewSet
general = General.objects.get(is_active=True)
File "django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "django\db\models\query.py", line 425, in get
num = len(clone)
File "django\db\models\query.py", line 269, in __len__
self._fetch_all()
File "django\db\models\query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "django\db\models\query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "django\db\models\sql\compiler.py", line 1156, in execute_sql
cursor.execute(sql, params)
File "django\db\backends\utils.py", line 98, in execute
return super().execute(sql, params)
File "django\db\backends\utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "django\db\backends\utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "env\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "env\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: HATA: "app_general" object doesnt exist
LINE 1: ...", "app_general"."field1" FROM "field_2...
views.py
class SampleViewSet(viewsets.ModelViewSet):
general = General.objects.get(is_active=True)
queryset = model.objects.filter(field=site_general.field)
i deleted all migration files in all apps (without __init__ ) and also .pyc files
is there any idea for this problem?
(edit i added Traceback)
All the non-method attributes of a class get evaluated /executed during the declaration or definition of the class. Hence when the interpreter reaches your class SampleViewSet it tries to execute the line general = General.objects.get(is_active=True) but of course your database doesn't exist yet and since calling .get implies a database query you get an error.
In general any statement that might cause a database query should be either run by the app configs ready method, by some middleware / context processor, or by the view in a request. Looking at the code it doesn't look like you use general anywhere, so you can simply remove it:
class SampleViewSet(viewsets.ModelViewSet):
queryset = model.objects.filter(field=site_general.field)
You might say model.objects.filter(...) would also cause a query, but .filter returns a QuerySet and queryset's are lazy and hence evaluated only when needed, so that will not cause any error.

How to address the following error on a Django Project: "django.contrib.auth.models.DoesNotExist: Group matching query does not exist."

I am trying to run migrations on my django project and I'm getting this really bizarre error that I haven't been able to address for a while now, and I don't understand it.
here's the error I'm getting:
Anaconda3\envs\mbt\lib\site-packages\django\db\models\query.py", line 415, in get
raise self.model.DoesNotExist(
django.contrib.auth.models.DoesNotExist: Group matching query does not exist.
I've tried to run python manage.py makemigrations
python manage.py migrate auth
python manage.py migrate my_app_name
I've also inserted the group names directly into my postgresql database just in case they're not posting. This error is preventing me from running the server locally.
I thought that the Group model was generated automatically and there wasn't much I needed to do to get started using it. It's definitely visible in my database.
any help would be greatly appreciated! thank you!!
here's the full traceback:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\nance\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\core\management\base.py", line 366, in execute
self.check()
File "C:\Users\nance\Anaconda3\lib\site-packages\django\core\management\base.py", line 395, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\nance\Anaconda3\lib\site-packages\django\core\management\base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "C:\Users\nance\Anaconda3\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 588, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 581, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\nance\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\nance\repo\project\app1\urls.py", line 28, in <module>
+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\nance\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\nance\repo\project\app1\urls.py", line 5, in <module>
from app1 import views
File "C:\Users\nance\repo\project\app1\views.py", line 188, in <module>
class AllMentorsView(TemplateView):
File "C:\Users\nance\repo\project\app1\views.py", line 192, in AllMentorsView
mentor_group = Group.objects.get(name="Mentor")
File "C:\Users\nance\Anaconda3\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\nance\Anaconda3\lib\site-packages\django\db\models\query.py", line 417, in get
self.model._meta.object_name
django.contrib.auth.models.DoesNotExist: Group matching query does not exist.

Django: manage.py makemigrations --> django.db.utils.OperationalError: no such table

What i did:
- deleted the complete db.
- deleted the content of the migrations folder
then i did:
python manage.py makemigrations.
I get the above error message.
I also did:
- Pulled the github repo freshly
- created venv
- activated the venv
- run python manage.py migrate
it looks like as if the migrate command checks the complete sourcefiles for references to the non existing tables. I am out of ideas here.
Error:
python manage.py makemigrations
Traceback (most recent call last):
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: newshows_setting
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/core/management/base.py", line 366, in execute
self.check()
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/core/management/base.py", line 395, in check
include_deployment_checks=include_deployment_checks,
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/core/management/base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 407, in check
for pattern in self.url_patterns:
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 588, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 581, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/harald/Projects/newshows/src/new_shows/urls.py", line 7, in <module>
path('', include('newshows.urls')),
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/harald/Projects/newshows/src/newshows/urls.py", line 2, in <module>
from . import views
File "/home/harald/Projects/newshows/src/newshows/views.py", line 7, in <module>
from .tables import ShowTable
File "/home/harald/Projects/newshows/src/newshows/tables.py", line 8, in <module>
class ShowTable(tables.Table):
File "/home/harald/Projects/newshows/src/newshows/tables.py", line 12, in ShowTable
class Meta:
File "/home/harald/Projects/newshows/src/newshows/tables.py", line 13, in Meta
settings = Setting.objects.get(id=1)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/models/query.py", line 411, in get
num = len(clone)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/models/query.py", line 258, in __len__
self._fetch_all()
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/models/query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/models/query.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1144, in execute_sql
cursor.execute(sql, params)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "/home/harald/Projects/newshows/.venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: newshows_setting
The problem is identified here in your stacktrace:
File "/home/harald/Projects/newshows/src/newshows/tables.py", line 13, in Meta
settings = Setting.objects.get(id=1)
This means that you're running Setting.objects.get(id=1) somewhere in your code as the application is compiling. This will occur before the making of actual migrations in the makemigrations command. Refactoring your code to do that call lazily is what will fix your problem.
Problem was, that i tried to access a queryset which wasn't there yet. I refactored the whole code to use environment variables for the settings.

Django Migration Problem with URL Configuration

When we started our Django project we created migrations and migrated, but now, when we want to alter the data model and run makemigrations we get a large error message (see below). After tracing back the message I discovered that when I comment the path('', include('quizarchiv.urls')), lie while running migrations everything is fine. So I guess that I got two questions:
Why does this workaround work (at least for creating the migrations)?
How do we adjust our project to work with migrations again?
This is the part of our models.py (it's rather long):
class questioncategory(models.Model):
cat_id = models.AutoField(primary_key=True)
cat_name = models.CharField(max_length=255, unique=True)
class question(models.Model):
q_id = models.AutoField(primary_key=True)
q_title = models.CharField(max_length=255, unique=True)
q_question = models.CharField(max_length=1000)
f_kat = models.ForeignKey(questioncategory, on_delete=models.PROTECT)
Full error message:
COMPUTER:FOLDER USERNAME$ python3 manage.py migrate
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: questioncategory
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 361, in execute
self.check()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 65, in _run_checks
issues.extend(super()._run_checks(**kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/urls/resolvers.py", line 571, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/urls/resolvers.py", line 564, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/USERNAME/PROJECTPATH/web/web/urls.py", line 22, in <module>
path('', include('quiz.urls')),
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/USERNAME/PROJECTPATH/web/quizarchiv/urls.py", line 2, in <module>
from . import views
File "/Users/USERNAME/PROJECTPATH/web/quizarchiv/views.py", line 20, in <module>
from .forms import ErstelleFrage, ErstelleSession, ErstelleAsset, ErstelleHinweis, ErstelleTeam, ErstelleKategorie
File "/Users/USERNAME/PROJECTPATH/web/quizarchiv/forms.py", line 8, in <module>
class ErstelleFrage(forms.Form):
File "/Users/USERNAME/PROJECTPATH/web/quizarchiv/forms.py", line 12, in ErstelleFrage
f_kategorie = forms.ChoiceField(choices=[(kat.kategorie_id, kat.k_name) for kat in FrageKategorie.objects.all()], label="Kategorie")
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 274, in __iter__
self._fetch_all()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1097, in execute_sql
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: questioncategory
The problem is not your url include. The problem is that in your ErstelleFrage form you are performing a query at class level, which is executed on import; it's just that the include is the thing that is causing that form to be imported.
You should never do queries in field or form definitions like this. In this particular case, you should be using ModelChoiceField with a queryset rather than ChoiceField with an explicitly selected set of choices.

No such table upon initial Django migration

Back story:
I'm creating a quiz app. I started by wanting to use Django (v=2.1.4) with Mongo via the Django package. Everything worked until it didn't so I decided to just drop Django and mongo entirely and go back to SQLite. I only mention it in case it is related, but I don't think it is.
I made a copy of the project then deleted the original. I then created a new project with the same name and all the same apps. This created a bunch of new folders with blank files. I moved the copies of the original over to these folders (except the migration files) and replaced the blank files with the same name.
I changed the mysite/settings.py file back to default to read:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'hoquidb'),
}
}
To my knowledge, this creates a fresh project ready for it's initial migration and it will use sqlite3.
The problem:
So I tried to do the initial migration and got a cryptic error message:
(base) C:[redacted]\testabode\Hoqui\hoqui>python manage.py migrate
Traceback (most recent call last):
File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:[redacted]\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: realestate_sentence_beginning
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:[redacted]\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:[redacted]\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:[redacted]\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:[redacted]\lib\site-packages\django\core\management\base.py", line 350, in execute
self.check()
File "C:[redacted]\lib\site-packages\django\core\management\base.py", line 379, in check
include_deployment_checks=include_deployment_checks,
File "C:[redacted]\lib\site-packages\django\core\management\commands\migrate.py", line 60, in _run_checks
issues.extend(super()._run_checks(**kwargs))
File "C:[redacted]\lib\site-packages\django\core\management\base.py", line 366, in _run_checks
return checks.run_checks(**kwargs)
File "C:[redacted]\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks
new_errors = check(app_configs=app_configs)
File "C:[redacted]\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:[redacted]\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:[redacted]\lib\site-packages\django\urls\resolvers.py", line 396, in check
for pattern in self.url_patterns:
File "C:[redacted]\lib\site-packages\django\utils\functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:[redacted]\lib\site-packages\django\urls\resolvers.py", line 533, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:[redacted]\lib\site-packages\django\utils\functional.py", line 37, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:[redacted]\lib\site-packages\django\urls\resolvers.py", line 526, in urlconf_module
return import_module(self.urlconf_name)
File "C:[redacted]\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:[redacted]\testabode\Hoqui\hoqui\hoqui\urls.py", line 24, in <module>
path('quiz_taker/', include('quiz_taker.urls')),
File "C:[redacted]\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:[redacted]\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "C:[redacted]\testabode\Hoqui\hoqui\quiz_taker\urls.py", line 3, in <module>
from . import views
File "C:[redacted]\testabode\Hoqui\hoqui\quiz_taker\views.py", line 2, in <module>
from quiz_maker.tests.generate_questions import main as question_list_func
File "C:[redacted]\testabode\Hoqui\hoqui\quiz_maker\tests\generate_questions.py", line 22, in <module>
sentence_beginning(text=first_words_of_sentence,category='Location').save()
File "C:[redacted]\lib\site-packages\django\db\models\base.py", line 718, in save
force_update=force_update, update_fields=update_fields)
File "C:[redacted]\lib\site-packages\django\db\models\base.py", line 748, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:[redacted]\lib\site-packages\django\db\models\base.py", line 831, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:[redacted]\lib\site-packages\django\db\models\base.py", line 869, in _do_insert
using=using, raw=raw)
File "C:[redacted]\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:[redacted]\lib\site-packages\django\db\models\query.py", line 1136, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:[redacted]\lib\site-packages\django\db\models\sql\compiler.py", line 1289, in execute_sql
cursor.execute(sql, params)
File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:[redacted]\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:[redacted]\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:[redacted]\lib\site-packages\django\db\backends\sqlite3\base.py", line 296, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: realestate_sentence_beginning
For some reason you're importing the test file quiz_maker.tests.generate_questions into your views. That file has code at the top level which tries to save an element into the database. But since that code is executed at import time, the migration won't have had a chance to run yet, and the table is not created.
You should not be doing either of those things: don't import test files into your actual code, and even in test files don't perform db-modifying actions at module level, they should be done inside functions that are called when necessary.