[Django]-Django.db.utils.ProgrammingError: relation "bot_trade" does not exist

46👍

You probably haven’t created any migrations for your bot app. You need to specify the app name to create the initial migrations:

./manage.py makemigrations bot

Then run migrate to run the migration and create the missing table:

./manage migrate

When you run showmigrations, you can see that Django thinks that it has already applied the initial migration for your bots app. This could be because you ran --fake for that app.

bots
 [X] 0001_initial

You can tell Django to mark the migrations as unapplied, then rerun the migration with:

manage.py migrate --fake bots zero
manage.py migrate bots

This should work, as long as no tables from the bots app have been created yet. If only some of the tables have been created, then fixing up the database will be much trickier.

5👍

I had a problem, and I really struggled to figure out what was wrong. I made an application with the Django framework. Everything worked fine on my local machine, including the DB. Only, when I put my application on the server, I wanted to migrate the database, but I had this error :

Traceback (most recent call last):
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "administration_parks" does not exist
LINE 1: ...name", "administration_parks"."availability" FROM "administr...
                                                             ^


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 "/home/thomas/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 75, in handle
    self.check(databases=[database])
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/management/base.py", line 419, in check
    all_issues = checks.run_checks(
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/checks/registry.py", line 76, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/thomas/env/lib/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/thomas/env/lib/python3.8/site-packages/django/urls/resolvers.py", line 412, in check
    for pattern in self.url_patterns:
  File "/home/thomas/env/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/thomas/env/lib/python3.8/site-packages/django/urls/resolvers.py", line 598, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/thomas/env/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/thomas/env/lib/python3.8/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/thomas/AnimauBoue/AnimauBoue/urls.py", line 21, in <module>
    url(r'^', include('administration.urls')),
  File "/home/thomas/env/lib/python3.8/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/thomas/AnimauBoue/administration/urls.py", line 4, in <module>
    from .views import index
  File "/home/thomas/AnimauBoue/administration/views.py", line 12, in <module>
    from .forms import ConnectionForm, UpdateDataForm, AddClientForm, SelectParkAndClientForm, DogForm, AddDog
  File "/home/thomas/AnimauBoue/administration/forms.py", line 26, in <module>
    class SelectParkAndClientForm(forms.Form):
  File "/home/thomas/AnimauBoue/administration/forms.py", line 31, in SelectParkAndClientForm
    for park in parks:
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/models/query.py", line 280, in __iter__
    self._fetch_all()
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/models/query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/models/query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/thomas/env/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "administration_parks" does not exist
LINE 1: ...name", "administration_parks"."availability" FROM "administr...

In fact, I came to understand that Django, when it migrates a DB, seems to perform some manipulation on views from urls.py of applications. And in my case, this was the problem. So the solution was:

  1. Comment all my url patterns, and then do the migrations, and there it worked !

  2. At this stage, it allowed me to create the tables present in my models. But Django’s default tables weren’t created since I no longer had any url schema. So I uncommented all the urls in urls.py, do a new pull on my server, and redo the migrations. And there, all the tables created by default by django have been created.

5👍

Another reason why you might face the same issue is because you’re trying to access datas earlier than creating them in your database. For example I wasn’t able to perform migrations because of this line of code:

cat_choices = [(cat, cat) for cat in Category.objects.all().values_list('title', flat=True)]

the problem is:

Category.objects.all().values_list()

The problem is that the code is trying to grab the values from model Category before the db table for category even exist. For this reason it wasn’t able to perform makemigrations and migrate. To solve this you can enclose the code in try except block with ProgrammingError and OperationalError as the exception.

try:
    cat_choices = Category.objects.all().values_list('title')
except (OperationalError, ProgrammingError) as e:
    cat_choices=[]

1👍

In case anyone else came across this question when looking for django.db.utils.ProgrammingError, be aware, that this question concerns does not exists issue. If you are looking for error relation already exists, you can find this question in link django.db.utils.ProgrammingError: relation already exists

And nice explanation can be found here https://dev.to/siumhossain/djangodbutilsprogrammingerror-column-of-relation-appnametable-already-exists-231g

Leave a comment