Задать вопрос
@AntonIgin

Django и PostgreSQL: Что делать с django.db.utils.ProgrammingError?

Написал проект на SQLite3 и решил настроить его на PostgreSQL, внеся в настройки следующее:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'company_base', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': 'postgres', # Not used with sqlite3.
'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
}
}

Установил psycopg:
pip install psycopg2

После того, как ввожу python3 manage.py migrate, выдает трейсбек, указанный ниже. Странный факт: если закоммитить приложение в Installed_apps, миграция пройдет нормально, но ошибка возвращается, стоит включить приложение обратно. С SQLite3 никаких проблем нет.

Трейсбек:
Traceback (most recent call last):
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "main_app_sector" does not exist
LINE 1: ...n_app_sector"."id", "main_app_sector"."name" FROM "main_app_...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/core/management/__init__.py", line 338, in execute
    django.setup()
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/apps/registry.py", line 116, in populate
    app_config.ready()
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/contrib/admin/apps.py", line 23, in ready
    self.module.autodiscover()
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/home/anton/virtenvs/com_env/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/anton/Django-u/company_base/main_app/admin.py", line 3, in <module>
    from .forms import *
  File "/home/anton/Django-u/company_base/main_app/forms.py", line 28, in <module>
    class ProfileFilterForm(forms.Form):
  File "/home/anton/Django-u/company_base/main_app/forms.py", line 30, in ProfileFilterForm
    sector = forms.ChoiceField(label='Отдел', required=False, choices=build_sectors_list(), widget=forms.Select(attrs={'name':'sector', 'style':'selector_element',}))
  File "/home/anton/Django-u/company_base/main_app/forms.py", line 18, in build_sectors_list
    for i in sectors:
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/db/models/query.py", line 250, in __iter__
    self._fetch_all()
  File "/home/anton/virtenvs/com_env/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/anton/virtenvs/com_env/lib/python3.5/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 894, in execute_sql
    raise original_exception
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 884, in execute_sql
    cursor.execute(sql, params)
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/anton/virtenvs/com_env/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/anton/virtenvs/com_env/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 "main_app_sector" does not exist
LINE 1: ...n_app_sector"."id", "main_app_sector"."name" FROM "main_app_...
  • Вопрос задан
  • 960 просмотров
Подписаться 1 Оценить 13 комментариев
Пригласить эксперта
Ответы на вопрос 1
igorfedun
@igorfedun
full-stack python developer
Целый ряд ошибок:
1. Если ты создал виртуальную среду то зачем вводить python3 manage.py migrate, можно просто python manage.py migrate
2. Смотри что ты прописал в моделях, проблема в связях.
3. makemigrations надеюсь выполнил?

>> "main_app_sector" - именно эту смотри запись
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы