I'm unable make any migrations from scratch with my current codebase.
Running ./manage.py makemigrations reports gives the following traceback
Traceback (most recent call last):
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "reports_frozenschedule" does not exist
LINE 1: ..._file", "reports_frozenschedule"."abandoned" FROM "reports_f...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/core/management/base.py", line 342, in execute
self.check()
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/urls/resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/jwe/Documents/piesup2/piesup2/urls.py", line 9, in <module>
from reports.api import viewsets
File "/home/jwe/Documents/piesup2/reports/api/viewsets.py", line 77, in <module>
class ActiveServerViewSet(ReadOnlyModelViewSet):
File "/home/jwe/Documents/piesup2/reports/api/viewsets.py", line 78, in ActiveServerViewSet
queryset = Server.active.all()
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/models/manager.py", line 160, in all
return self.get_queryset()
File "/home/jwe/Documents/piesup2/reports/models.py", line 550, in get_queryset
fqdn__in=active_server_fqdns()
File "/home/jwe/Documents/piesup2/reports/models.py", line 541, in active_server_fqdns
f.deserialize.servers for f in FrozenSchedule.objects.active()
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/models/query.py", line 256, in __iter__
self._fetch_all()
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/models/query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql()
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/jwe/Documents/piesup2/venv/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "reports_frozenschedule" does not exist
LINE 1: ..._file", "reports_frozenschedule"."abandoned" FROM "reports_f...
Following the stack trace, the breaking starts when Djagno scans my urls.py file, which looks like:
from django.conf.urls import include, url
from rest_framework import routers
from reports.api import viewsets
router = routers.DefaultRouter()
router.register(r'active-servers', viewsets.ActiveServerViewSet)
...
urlpatterns = [
url(r'^api/', include(router.urls)),
url(r'^', include('reports.urls')),
]
It reaches from reports.api import viewsets where it imports the following:
viewsets.py
from reports.models import Server
...
class ActiveServerViewSet(ReadOnlyModelViewSet):
queryset = Server.active.all()
...
It makes it to Server.active.all() which relies on a custom Model Manager shown here:
models.py
class ActiveServerManager(models.Manager):
def get_queryset(self):
return super(ActiveServerManager, self).get_queryset().filter(
fqdn__in=active_server_fqdns()
)
class Server(models.Model):
fqdn = models.CharField(....)
....
objects = models.Manager()
active = ActiveServerManager()
My server model manager calls the fucntion active_server_fqdns() which looks as follows:
def active_server_fqdns():
# Returns flat set of FQDN's associated with all currently active Schedules
return {server.fqdn for servers in [
f.deserialize.servers for f in FrozenSchedule.objects.active()
] for server in servers}
This function envokes FrozenSchedule.objects.active() inside the set comprehension, which is where everything explodes
The custom queryset FrozenSchedule.objects.active() is shown below.
Here Django complains that the relation "reports_frozenschedule" does not exist
class FrozenScheduleQuerySet(models.QuerySet):
def active(self):
return self.filter(
Q(start_date__lte=Now()), Q(end_date__gte=Now()) | Q(end_date=None)
)
class FrozenSchedule(models.Model):
....
objects = FrozenScheduleQuerySet.as_manager()
Why would my model managers be causing my database migrations to fail?
It appears there is some kind of race condition occuring from urls.py resulting in the queryset beign evaluated before the table has actually been registered.
Update with fix
The fix was, as suggested to get rid of the module level query. I've moved it into the get_queryset() method provided by DjangoRestFramework, like:
class ActiveServerViewSet(ReadOnlyModelViewSet):
serializer_class = serializers.LinkedServerSerializer
...
def get_queryset(self):
return Server.active.all()
Then since it no longer has a queryset attribute I had to add a base_name argument to my viewset
router.register(r'active-servers', viewsets.ActiveServerViewSet, 'Server')
This is caused by module-level code interacting with the database:
class ActiveServerViewSet(ReadOnlyModelViewSet):
queryset = Server.active.all()
This code is run at module-loading time when you run makemigrations and, as you have laid out, leads to a FrozenSchedule QuerySet being evaluated (for f in FrozenSchedule.objects.active()) when the according database table does not exist yet.
You should generally avoid having module-level code (that includes class definitions outside of methods) doing db queries as this code is executed upon module import which is often when you start the server or run a management command.
See this ticket for example:
you should never execute queries at module level
Related
I am trying to split my django settings into production and development. Th ebiggest question that I have is how to use two different databases for the two environments? How to deal with migrations?
I tried changing the settings for the development server to use a new empty database, however, I can not apply the migrations to create the tables that I already have in the production database.
All the guides on multiple databases focus on the aspect of having different types of data in different databases (such as users database, etc.) but not the way I am looking for.
Could you offer some insights about what the best practices are and how to manage the two databases also in terms of migrations?
EDIT:
Here is what I get when I try to run python manage.py migrate on the new database:
Traceback (most recent call last):
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "dashboard_posttags" does not exist
LINE 1: ...ags"."tag", "dashboard_posttags"."hex_color" FROM "dashboard...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/....../manage.py", line 22, in <module>
main()
File "/....../manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
utility.execute()
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/management/base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/management/base.py", line 417, in execute
output = self.handle(*args, **options)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/management/base.py", line 90, in wrapped
res = handle_func(*args, **kwargs)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 75, in handle
self.check(databases=[database])
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/management/base.py", line 438, in check
all_issues = checks.run_checks(
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/checks/registry.py", line 77, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/urls/resolvers.py", line 446, in check
for pattern in self.url_patterns:
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/urls/resolvers.py", line 632, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/urls/resolvers.py", line 625, in urlconf_module
return import_module(self.urlconf_name)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/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 "/....../app/urls.py", line 11, in <module>
from main_platform.views.investor import AccountView, profile, app_home_redirect
File "/....../main_platform/views/investor.py", line 118, in <module>
class PostFilter(django_filters.FilterSet):
File "/....../main_platform/views/investor.py", line 124, in PostFilter
for tag in PostTags.objects.all():
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/db/models/query.py", line 280, in __iter__
self._fetch_all()
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/auto_prefetch/__init__.py", line 98, in _fetch_all
super()._fetch_all()
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/db/models/query.py", line 1354, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/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 "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1202, in execute_sql
cursor.execute(sql, params)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/sentry_sdk/integrations/django/__init__.py", line 563, in execute
return real_execute(self, sql, params)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/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 "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/opt/homebrew/Caskroom/miniforge/base/envs/stokk/lib/python3.9/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "dashboard_posttags" does not exist
LINE 1: ...ags"."tag", "dashboard_posttags"."hex_color" FROM "dashboard...
If you have a new empty database, you can just run "python manage.py migrate" and all migrations will be executed on the new database. The already done migrations will be stored in a table in that database so that django always "remembers" the migrations state of each individual database. Of course that new database will only have the tables structure - there is not yet any data copied!
Does this answer your question?
Found the solution in this post
Apparently, Django tries to evaluate all queries declared on class level before running the migration. This causes an error because the database is empty, and the query can not run.
To solve this, comment out everything in the main urls.py, so that there are no imports and urlpatterns. Then run python manage.py makemigrations and after it has completed, revert the urls.py to the original.
I have registered the models in admin.py
the whole things I've changed in this app are in admin.py and models.py
I didn't created the urls.py
When I commented the registration in admin.py i was able to migrate the models.py changes but now when i registering the models with admin i can't use runserver and to create objects i should use the shell command
i just wanted to add objects from admin not to use the shell and i think if i don't debug this will be a problem sometime later again
this is my models.py:
from django.db import models
# Create your models here.
class Clients(models.Model):
name = models.CharField(max_length=300)
company = models.CharField(max_length=300)
def __str__(self) -> str:
return self.company
class Manufacturers(models.Model):
name = models.CharField(max_length=300)
location = models.TextField("address")
def __str__(self) -> str:
return self.name
class Products(models.Model):
cost_per_item = models.PositiveBigIntegerField("Cost")
name_of_product = models.CharField("name", max_length=300)
manufacturer = models.ForeignKey(Manufacturers, on_delete=models.CASCADE)
def __str__(self) -> str:
return self.name
class ClientOrders(models.Model):
fulfill_date = models.PositiveIntegerField("Fulfill Month")
order_number = models.PositiveIntegerField(primary_key=True)
client = models.ForeignKey(Clients, on_delete=models.CASCADE)
products = models.ManyToManyField(Products)
def __str__(self) -> str:
return self.client
this is admin.py:
from django.contrib import admin
from .models import Clients, ClientOrders, Manufacturers, Products
# Register your models here.
admin.site.register(Clients, ClientOrders)
admin.site.register( Manufacturers, Products)
this is the traceback:
(Django_learn) D:\learnD\learnF1>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner
self.run()
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Armin\AppData\Local\Programs\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 "D:\learnD\learnF1\config\urls.py", line 20, in <module>
path('admin/', admin.site.urls),
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\functional.py", line 247, in inner
return func(self._wrapped, *args)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\sites.py", line 299, in urls
return self.get_urls(), 'admin', self.name
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\sites.py", line 279, in get_urls
path('%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
AttributeError: 'ClientOrders' object has no attribute 'urls'
when I was trying to make migrations: python manage.py makemigrations
The following error occurred: django.db.utils.OperationalError: no such table: blog_category
Traceback (most recent call last):
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django \db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: blog_category
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:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django \core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 361, in execute
self.check()
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 571, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\resolvers.py", line 564, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\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:\xampp\htdocs\Django project\blooper\Bona-Blog-master\bona_blog\urls.py", line 27, in <module>
path('', include('blog.urls', namespace='blog')), # Urls for article app.
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\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:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\urls.py", line 5, in <module>
from blog.views.blog.article_views import (
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\views\blog\article_views.py", line 25, in <module>
from blog.forms.blog.article_forms import (
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\forms\blog\article_forms.py", line 16, in <module>
class ArticleCreateForm(forms.ModelForm):
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\forms\blog\article_forms.py", line 18, in ArticleCreateForm
class Meta:
File "C:\xampp\htdocs\Django project\blooper\Bona-Blog-master\blog\forms\blog\article_forms.py", line 47, in Meta
"data-live-search": "true"
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\forms\widgets.py", line 562, in __init__
self.choices = list(choices)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 274, in __iter__
self._fetch_all()
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\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 "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\compiler.py", line 1097, in execute_sql
cursor.execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "C:\Users\injemam\AppData\Local\Programs\Python\Python37-32\lib\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: blog_category
Please Help How to remove this error?
I'm new in Django and I had clone this project from Github
https://github.com/Williano/Bona-Blog
using Django-2.2
Apparantly,
Either you have changed your db tables manually (e.g. via db interface)
You are missing one or more of required migration files.
As you are on a development environment I would suggest
drop the db
re-create the db
then python manage.py migrate again
This seams to be a bug in the blog software.
When the migrations are created the models in the code are introspected and in this process many modules are imported with the models. In the software there is a point where a query is made at import time (article_forms.py, line 40. At this point the table the query points to does not exist yet and it fails.
It is possible the author did not notice the bug because he/she had the table already when doing the change. And he/she never created a new instance from scratch.
You should report it as a bug.
Since you are in development stage you can drop the db and recreate it. But before recreating delete all the previous migration files on all the apps. Make sure that there are only lnlt files in those folders
python manage.py makemigrations
python manage.py migrate
This fixed it for me!
This question is old but I am putting a solution that worked for me as no one else has mentioned it yet. Also this could have just been a comment but I need 50 reputations to pull a comment, which I do not have at the time of writing this.
Instead of running project-wide migrations, make it specific to the blog app:
python manage.py makemigrations blog
python manage.py migrate blog
Do this for your other apps.
I am developing a Django project with REST Framework. It all worked well before, but all of a sudden I can't add any new model fields:
If I add any new field even if the most simple one like this:
def_episode = models.IntegerField(blank=True, null=True)
And manage.py makemigrations, this will happen:
Traceback (most recent call last):
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: novelrecorder_novel.def_episode
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 "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\base.py", line 361, in execute
self.check()
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\urls\resolvers.py", line 584, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\urls\resolvers.py", line 577, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\daichou\AppData\Local\Programs\Python\Python37-32\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 "D:\MyProjects\Python\Django\env\mysite\mysite\urls.py", line 19, in <module>
from novelrecorder import views
File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\views.py", line 19, in <module>
from novelrecorder.serializers import NovelSerializer, NovelReadOnlySerializer, \
File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\serializers.py", line 241, in <module>
class CharacterCreateSerializer(CharacterSerializer):
File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\serializers.py", line 245, in CharacterCreateSerializer
param_key_field_name='novel_id'))
File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\yd_fields.py", line 23, in __init__
super().__init__(**kwargs)
File "D:\MyProjects\Python\Django\env\mysite\novelrecorder\yd_fields.py", line 10, in __init__
super().__init__(**kwargs)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\rest_framework\fields.py", line 1404, in __init__
self.choices = choices
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\rest_framework\fields.py", line 1440, in _set_choices
self.grouped_choices = to_choices_dict(choices)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\rest_framework\fields.py", line 142, in to_choices_dict
for choice in choices:
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\models\query.py", line 274, in __iter__
self._fetch_all()
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "D:\MyProjects\Python\Django\env\venv\lib\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 "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 99, in execute
return super().execute(sql, params)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\MyProjects\Python\Django\env\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:\MyProjects\Python\Django\env\venv\lib\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 column: novelrecorder_novel.def_episode
Without adding any new fields, everything looks ok and the server works.
Tried to remove the field and makemigrations then migrate, "Nothing needs to change or apply"
Parts of settings.py:
INSTALLED_APPS = [
'novelrecorder.apps.NovelRecorderConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'rest_framework',
'gunicorn',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
The model I attempted to add a new field model.py:
class Novel(CustomNovelModel):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, related_name='novel_author')
name = models.CharField(max_length=200)
is_public = models.BooleanField(default=True)
def_episode = models.IntegerField(blank=True, null=True)
class Meta:
ordering = ['name']
unique_together = ['author', 'name']
I was searching for a similar problem and came across you question.
After reading through my error-log again I noticed that the migrations was stumbling on some frontend and admin code which was causing it to fail.
To 'fix' the issue
I temporary commented-out the code in question
Ran migrations
Deployed everything
Uncommented the code
All seems to be fine.
When I'm looking through your log, I'm seeing a package novelrecorder which seems to cause an error. Try commenting-out that code.
Perhaps this helps you or someone else coming across this issue.
Hey I am attempting to initialize a new database, but I am running into some issues setting up the migrations. The error I am getting appears to stem from setting up my forms. In a form I am using, I am creating a choice field as so:
from django import forms
from ..custom_admin import widgets, choices
class MemberForm(forms.Form):
provinces = forms.ChoiceField(label='Provinces', choices=choices.PROVINCE_CHOICES, required=True)
where PROVINCE_CHOICES comes from here:
from ..base.models import ProvinceCode
PROVINCE_CHOICES = []
for province in ProvinceCode.objects.filter(country_code_id=1).order_by('code'):
PROVINCE_CHOICES.append((province.code, province.code))
The issue seems to be that this loop is being called before the migrations occur, giving me an error stating that the Province model does not exist. Commenting out the reference to this file allows the migrations to work, however, that seems like an impractical solution for continued use. Is there a way to get around this error?
For reference, here is the error I get when I run manage.py makemigrations:
./manage.py makemigrations
Traceback (most recent call last):
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "pc_psr_code" does not exist
LINE 1: ...escription", "pc_psr_code"."country_code_id" FROM "pc_psr_co...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./manage.py", line 9, in <module>
django.setup()
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/debug_toolbar/apps.py", line 15, in ready
dt_settings.patch_all()
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/debug_toolbar/settings.py", line 228, in patch_all
patch_root_urlconf()
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/debug_toolbar/settings.py", line 216, in patch_root_urlconf
reverse('djdt:render_panel')
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 568, in reverse
app_list = resolver.app_dict[ns]
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 360, in app_dict
self._populate()
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 293, in _populate
for pattern in reversed(self.url_patterns):
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/js/Documents/app/platform/test/pc/urls.py", line 7, in <module>
from .custom_admin import urls as custom_urls
File "/Users/js/Documents/app/platform/test/pc/custom_admin/urls.py", line 3, in <module>
from ..party import views as party_views
File "/Users/js/Documents/app/platform/test/pc/party/views.py", line 1, in <module>
from ..party import forms
File "/Users/js/Documents/app/platform/test/pc/party/forms.py", line 2, in <module>
from ..custom_admin import widgets, choices
File "/Users/js/Documents/app/platform/test/pc/custom_admin/choices.py", line 9, in <module>
for province in ProvinceCode.objects.filter(country_code_id=1).order_by('code'):
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/js/Documents/VirtualEnvironments/pcenv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "pc_psr_code" does not exist
LINE 1: ...escription", "pc_psr_code"."country_code_id" FROM "pc_psr_co...
Province model:
class ProvinceCode(models.Model):
code = models.CharField(blank=False, null=False, unique=True)
country_code = models.ForeignKey('CountryCode', blank=False, null=True)
You cannot execute queries during the initialization of the app registry. Your choices.py file is indirectly imported during this time, resulting in the error. To fix this issue, you can pass a callable to choices:
def get_provinces():
province_choices = []
for province in ProvinceCode.objects.filter(country_code_id=1).order_by('code'):
province_choices.append((province.code, province.code))
return province_choices
class MemberForm(forms.Form):
provinces = forms.ChoiceField(label='Provinces', choices=get_provinces, required=True)