django.db.utils.programmingerror:与递归模型不存在关系

nzk0hqpo  于 2021-09-08  发布在  Java
关注(0)|答案(2)|浏览(405)

我有一个django应用程序,可以在本地pc上正常工作。但当我将其部署到heroku时,它会打印消息:
django.db.utils.programmingerror:关系“核心菜单选项”不存在
现在,我搜索了很多,但是没有一个案例和我的相似。我认为我的问题在于我的模型 MenuOption 是可重复的。模型如下:

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

    ...

如您所见,参数 parent 是此模型的外键。因此,我认为由于模型尚未创建,django不知道与哪个模型相关。
我考虑过删除这个字段,然后迁移,然后把它带回来,但是太多的事情依赖于这个字段。
完全回溯:

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...

菜单选项窗体类:

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})
        }
rkue9o1l

rkue9o1l1#

问题是您的迁移从未被应用,事实上,您在运行时也应该遇到同样的错误 migrate 在服务器上。这里的问题是,您有以下行:

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

这一行自然会对数据库进行查询,那么这有什么错呢?该行在类运行时执行 MenuOptionForm 这是以前发生过的事情 migrate (因此出现了错误,迁移还没有发生)实际上已经运行了,因为在此之前所有文件都已导入。另一个问题是,除非重新加载服务器,否则此表单中的选项将永远不会更改,这可能不是您想要的。
您需要做的是不在此处传递选项,因为您没有手动指定表单字段。此处使用的字段是 ModelChoiceField 它将自行设置选项:

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})
        }

现在我们需要将更改迁移到数据库,首先确保将迁移提交到存储库,并将提交推送到heroku。接下来,如果您正在使用heroku cli,则只需运行:

heroku run python manage.py migrate
rggaifut

rggaifut2#

在@abdulazizbarkat的帮助下,我发现问题出在menuoptionform类中。我试图将menuoption类的所有对象附加到选项中,但该类未合并。因此,我将选项列表更改为:

...
'parent': forms.Select(attrs={'class': 'form-control mb-1'}, choices=["1","2","3"]),
...

然后heroku可以为我创建表格,然后我把它改回来,一切都很好!

相关问题