django.db.utils.ProgrammingError: relation does not exist with recursive model

D4NieLDev

I have a django app that is working as intended on my local pc. but when I'm deploying it to heroku it prints the message:

django.db.utils.ProgrammingError: relation "core_menuoption" does not exist

Now, I searched about this a lot, but no case is similar as mine. I think that my problem is because my model MenuOption is recuesive. Here is the model:

class MenuOption(models.Model):
    parent = models.ForeignKey('self', on_delete=models.CASCADE, blank=True, null=True)
    title = models.CharField(max_length=50, unique=True)
    
    ...

As you can see, the parameter parent is a foreign key to this model. So I think becuase the model is not yet created, django doesn't know which model to relate to.

I thought about maybe deleting this field, migrate and than bring it back, but too many things rely on this field.

Full traceback:

remote: Traceback (most recent call last):
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
remote:     return self.cursor.execute(sql, params)
remote: psycopg2.errors.UndefinedTable: relation "core_menuoption" does not exist
remote: LINE 1: ...tion"."parent_id", "core_menuoption"."title" FROM "core_menu...
remote:                                                              ^
remote:
remote:
remote: The above exception was the direct cause of the following exception:
remote:
remote: Traceback (most recent call last):
remote:   File "/app/manage.py", line 22, in <module>
remote:     main()
remote:   File "/app/manage.py", line 18, in main
remote:     execute_from_command_line(sys.argv)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
remote:     utility.execute()
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
remote:     self.fetch_command(subcommand).run_from_argv(self.argv)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
remote:     self.execute(*args, **cmd_options)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
remote:     output = self.handle(*args, **options)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 89, in wrapped
remote:     res = handle_func(*args, **kwargs)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 75, in handle
remote:     self.check(databases=[database])
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 419, in check
remote:     all_issues = checks.run_checks(
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/registry.py", line 76, in run_checks
remote:     new_errors = check(app_configs=app_configs, databases=databases)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
remote:     all_namespaces = _load_all_namespaces(resolver)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
remote:     url_patterns = getattr(resolver, 'url_patterns', [])
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
remote:     res = instance.__dict__[self.name] = self.func(instance)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/urls/resolvers.py", line 598, in url_patterns
remote:     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
remote:     res = instance.__dict__[self.name] = self.func(instance)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/urls/resolvers.py", line 591, in urlconf_module
remote:     return import_module(self.urlconf_name)
remote:   File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
remote:     return _bootstrap._gcd_import(name[level:], package, level)
remote:   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
remote:   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
remote:   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
remote:   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
remote:   File "<frozen importlib._bootstrap_external>", line 855, in exec_module
remote:   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
remote:   File "/app/ExplosiveArea/urls.py", line 20, in <module>
remote:     path('', include('core.urls')),
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/urls/conf.py", line 34, in include
remote:     urlconf_module = import_module(urlconf_module)
remote:   File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
remote:     return _bootstrap._gcd_import(name[level:], package, level)
remote:   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
remote:   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
remote:   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
remote:   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
remote:   File "<frozen importlib._bootstrap_external>", line 855, in exec_module
remote:   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
remote:   File "/app/core/urls.py", line 3, in <module>
remote:     from . import views
remote:   File "/app/core/views.py", line 11, in <module>
remote:     from core.forms import UserForm, MenuOptionForm, ExerciseForm
remote:   File "/app/core/forms.py", line 20, in <module>
remote:     class MenuOptionForm(forms.ModelForm):
remote:   File "/app/core/forms.py", line 21, in MenuOptionForm
remote:     class Meta:
remote:   File "/app/core/forms.py", line 25, in Meta
remote:     'parent': forms.Select(attrs={'class': 'form-control mb-1'}, choices=[(m, m) for m in MenuOption.objects.all()]),
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 280, in __iter__
remote:     self._fetch_all()
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 1324, in _fetch_all
remote:     self._result_cache = list(self._iterable_class(self))
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 51, in __iter__
remote:     results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
remote:     cursor.execute(sql, params)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
remote:     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
remote:     return executor(sql, params, many, context)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
remote:     return self.cursor.execute(sql, params)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
remote:     raise dj_exc_value.with_traceback(traceback) from exc_value
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
remote:     return self.cursor.execute(sql, params)
remote: django.db.utils.ProgrammingError: relation "core_menuoption" does not exist
remote: LINE 1: ...tion"."parent_id", "core_menuoption"."title" FROM "core_menu...

MenuOptionForm class:

class MenuOptionForm(forms.ModelForm):
    class Meta:
        model = MenuOption
        fields = ('parent', 'title')
        widgets = {
            'parent': forms.Select(attrs={'class': 'form-control mb-1'}, choices=[(m, m) for m in MenuOption.objects.all()]),
            'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'title})
        }
Abdul Aziz Barkat

The problem is your migrations never got applied, you in fact should have gotten this same error when you ran migrate on the server. The issue here is you have the following line:

'parent': forms.Select(attrs={'class': 'form-control mb-1'}, choices=[(m, m) for m in MenuOption.objects.all()]),

This line naturally makes a query to the database, well what's wrong with that? This line is executed at the time the class MenuOptionForm is made, which happens even before migrate (hence the error, the migration has not even happened yet) actually gets run since all your files are imported before that. Another issue with this is that your options in this form will never change unless you reload the server, which is likely not what you intend to happen.

What you need to do is not pass the choices here, Since you haven't specified the form field manually the field used here is a ModelChoiceField which will take care of setting the choices by itself:

class MenuOptionForm(forms.ModelForm):
    class Meta:
        model = MenuOption
        fields = ('parent', 'title')
        widgets = {
            'parent': forms.Select(attrs={'class': 'form-control mb-1'}),
            'title': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'title})
        }

Now we need to migrate the changes to the database, first make sure your migrations are commited to your repository and the commit is pushed to heroku. Next if you are using the Heroku CLI then you can simply run:

heroku run python manage.py migrate

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Steps to Troubleshoot "django.db.utils.ProgrammingError: permission denied for relation django_migrations"

ProgrammingError: relation "django_session" does not exist error after installing Psycopg2

ProgrammingError at / relation "main_post" does not exist

ProgrammingError: relation "django_site" does not exist

django.db.utils.ProgrammingError: relation already exists on OenBSD vps

django.db.utils.OperationalError: FATAL: role "django" does not exist

django.db.utils.ProgrammingError: relation "bot_trade" does not exist

django.db.utils.ProgrammingError: Table doesn't exist

"django.db.utils.ProgrammingError: relation "auth_user" does not exist" Django V2.0

ProgrammingError: relation "django_session" does not exist

django.db.utils.ProgrammingError: relation "django_site_domain_v2339b81_uniq" already exists

django.db.utils.ProgrammingError: relation "django_content_type" does not exist

django.db.utils.ProgrammingError: Could not load : column "" of relation "" does not exist

django.db.utils.ProgrammingError: (1146, "Table 'med_portal.Custparent' doesn't exist")

django.db.utils.ProgrammingError: column calculator_calculation._id does not exist using heroku and djongo

django.db.utils.ProgrammingError: type "raster" does not exist

Python Django - Internal Error ProgrammingError relation does not exist

Django test fails with 'django.db.utils.ProgrammingError: relation "django_content_type" does not exist'

django.db.utils.ProgrammingError: relation "..." does not exist

Getting error while syncdb django.db.utils.ProgrammingError: permission denied for relation django_migrations

django.db.utils.ProgrammingError: type "int4range" does not exist

Django Rest Framework "django.db.utils.ProgrammingError: relation "patient" does not exist"

django.db.utils.ProgrammingError: column am.amcanorder does not exist

Django psycopg2.ProgrammingError relation does not exist using custom model Manager

django.db.utils.ProgrammingError: column "image" of relation "choices_keyword" already exists

One more "django.db.utils.ProgrammingError: relation "device_gclouddevice" does not exist"

django.db.utils.ProgrammingError: relation "company_company" does not exist when running makemigrations

Django - ProgrammingError - column does not exist

"django.db.utils.ProgrammingError: relation already exists" for unique together constraint