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.
Related
hi guys i'm working in project with cms called tendenci which required to work with PostgreSQL and python django, I've been working on it on a virtual machine with Ubuntu and when i tried to migrate to production server (with Ubuntu also) i faced a lot of issues the last is migrate the db, actually i did all the required steps but it still showing an error when i try to run the server
this is the command that i run
python manage.py runserver
this is the error that i get
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fadbc707a60>
Traceback (most recent call last):
File "/srv/mysite/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "site_settings_setting" does not exist
LINE 1: SELECT (1) AS "a" FROM "site_settings_setting" WHERE ("site_...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/srv/mysite/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/srv/mysite/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 124, in inner_run
self.check(display_num_errors=True)
File "/srv/mysite/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/srv/mysite/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/srv/mysite/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/srv/mysite/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/srv/mysite/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/srv/mysite/lib/python3.6/site-packages/django/urls/resolvers.py", line 256, in check
for pattern in self.url_patterns:
File "/srv/mysite/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/srv/mysite/lib/python3.6/site-packages/django/urls/resolvers.py", line 407, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/srv/mysite/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/srv/mysite/lib/python3.6/site-packages/django/urls/resolvers.py", line 400, in urlconf_module
return import_module(self.urlconf_name)
File "/srv/mysite/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 "/var/www/clients/client1/web1/web/mysite/conf/urls.py", line 6, in <module>
from tendenci.urls import handler500 # noqa: F401
File "/srv/mysite/lib/python3.6/site-packages/tendenci/urls.py", line 18, in <module>
registry_autodiscover()
File "/srv/mysite/lib/python3.6/site-packages/tendenci/apps/registry/register.py", line 21, in autodiscover
import_module('%s.app_registry' % app)
File "/srv/mysite/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/srv/mysite/lib/python3.6/site-packages/tendenci/apps/entities/app_registry.py", line 18, in <module>
site.register(Entity, EntityRegistry)
File "/srv/mysite/lib/python3.6/site-packages/tendenci/apps/registry/sites.py", line 157, in register
cache_reg_apps(self.get_registered_apps())
File "/srv/mysite/lib/python3.6/site-packages/tendenci/apps/registry/sites.py", line 177, in get_registered_apps
apps = RegisteredApps(self._registry)
File "/srv/mysite/lib/python3.6/site-packages/tendenci/apps/registry/sites.py", line 44, in __init__
if check_setting(*setting_tuple):
File "/srv/mysite/lib/python3.6/site-packages/tendenci/apps/site_settings/utils.py", line 156, in check_setting
scope_category=scope_category, name=name).exists()
File "/srv/mysite/lib/python3.6/site-packages/django/db/models/query.py", line 673, in exists
return self.query.has_results(using=self.db)
File "/srv/mysite/lib/python3.6/site-packages/django/db/models/sql/query.py", line 517, in has_results
return compiler.has_results()
File "/srv/mysite/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 858, in has_results
return bool(self.execute_sql(SINGLE))
File "/srv/mysite/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 899, in execute_sql
raise original_exception
File "/srv/mysite/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 889, in execute_sql
cursor.execute(sql, params)
File "/srv/mysite/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/srv/mysite/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/srv/mysite/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/srv/mysite/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/srv/mysite/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "site_settings_setting" does not exist
LINE 1: SELECT (1) AS "a" FROM "site_settings_setting" WHERE ("site_...
i don't know if there is a problem with the db or there is somthing else
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 have a working local environment using Django 1.11 with a PostgreSQL 10 database running on Linux Mint.
I am trying to take a copy of this environment and install it on two Ubuntu 16.04 development servers: one for the web server and the other for the SQL server.
The problem is running Django with the Ubuntu PostgreSQL server.
When I take the Django settings.py file, and change the server from 'localhost' to the IP address of the new PostgreSQL server, every command issued with manage.py fails. This includes createsuperuser, makemigrations and migrate. This fails on both my working, development machine and the new Ubuntu web server.
Is there a step I am missing that should be done after changing the database server and before running a manage.py command?
Everything I have read says to simply run the makemigrations, and migrate commands. I have even tried to delete the makemigrations folders, but still no luck.
Verified: Can connect to the new PostgreSQL server remotely using the same user ID and password, and was able to create a table (so problem should not be related to security or remote access).
Here is the error that comes up each time:
Traceback (most recent call last):
File "/home/username/Documents/pycharm-2017.2.3/helpers/pycharm/django_manage.py", line 43, in <module>
run_module(manage_file, None, '__main__', True)
File "/usr/lib/python3.5/runpy.py", line 196, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "/usr/lib/python3.5/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/dcripps/PycharmProjects/seamus/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/base.py", line 327, in execute
self.check()
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 62, in _run_checks
issues.extend(super(Command, self)._run_checks(**kwargs))
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/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 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/dcripps/PycharmProjects/seamus/seamus/urls.py", line 21, in <module>
from assessments import views as assessment_views
File "/home/dcripps/PycharmProjects/seamus/assessments/views.py", line 10, in <module>
from .forms import NewForm, CreateForm, DetailForm, EquipmentDetailForm, EquipmentAddForm, InspectionsForm, \
File "/home/dcripps/PycharmProjects/seamus/assessments/forms.py", line 17, in <module>
.values_list('item_number', 'number_type'))
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/query.py", line 138, in __iter__
for row in compiler.results_iter():
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 836, in results_iter
results = self.execute_sql(MULTI)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 894, in execute_sql
raise original_exception
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 884, in execute_sql
cursor.execute(sql, params)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/dcripps/seamus_virt/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "EquipmentTypes" does not exist
LINE 1: ...ntTypes"."equipment_type")) AS "number_type" FROM "Equipment..
Thank you.
Firewall on SQL Server:
:~$ sudo ufw status
Status: active
To Action From
-- ------ ----
5432 ALLOW Anywhere
Here is what I added to the pg_hba.conf file:
host all all 0.0.0.0/0 md5
Databases in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '******',
'USER': '******',
'PASSWORD': '******',
'HOST': '192.168.1.221',
'PORT': '5432',
}
}
As per Daniel, the error came from here:
item_number_list = [("", "---------")] + \
list(EquipmentTypes.objects.all()
.annotate(number_type=Concat('item_number', Value(' - '), 'equipment_type'))
.values_list('item_number', 'number_type'))
I was using this as a way to populate a dropdown list in a model:
'item_number': forms.Select(choices=item_number_list, attrs={'class': 'form-control'}),
I commented out this code, and everything worked perfectly.
The traceback is showing that the problem is caused by line 17 of assessment/forms.py - we can't see the actual code but it seems likely that you are performing a query at class or module level. You must not do this, as anything at that level is executed on import, which is before the migrations can have a chance to run.
Note that this problem is not related to running the database on a separate server specifically, but just caused by moving to a new empty database.
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
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)