django.db.utils.ProgrammingError: relation does not exist with recursive model
django.db.utils.ProgrammingError: relation does not exist with recursive model
我有一个 Django 应用程序,它在我的本地电脑上按预期运行。但是当我将它部署到 heroku 时,它会打印消息:
django.db.utils.ProgrammingError: relation "core_menuoption" does not exist
现在,我搜索了很多,但没有一个案例与我的相似。我认为我的问题是因为我的模型 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...
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})
}
在@AbdulAzizBarkat 的帮助下,我发现问题出在 MenuOptionForm class 中。我试图将 MenuOption class 的所有对象附加到选项中,但 class 未迁移。
所以我将选择列表更改为:
...
'parent': forms.Select(attrs={'class': 'form-control mb-1'}, choices=["1","2","3"]),
...
heroku 可以为我创建表格,然后我将其改回原样,一切正常!
问题是您的迁移从未得到应用,实际上您在服务器上 运行 migrate
时应该遇到同样的错误。这里的问题是您有以下行:
'parent': forms.Select(attrs={'class': 'form-control mb-1'}, choices=[(m, m) for m in MenuOption.objects.all()]),
这一行自然地查询了数据库,这有什么问题吗?此行在 class 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
我有一个 Django 应用程序,它在我的本地电脑上按预期运行。但是当我将它部署到 heroku 时,它会打印消息:
django.db.utils.ProgrammingError: relation "core_menuoption" does not exist
现在,我搜索了很多,但没有一个案例与我的相似。我认为我的问题是因为我的模型 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...
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})
}
在@AbdulAzizBarkat 的帮助下,我发现问题出在 MenuOptionForm class 中。我试图将 MenuOption class 的所有对象附加到选项中,但 class 未迁移。 所以我将选择列表更改为:
...
'parent': forms.Select(attrs={'class': 'form-control mb-1'}, choices=["1","2","3"]),
...
heroku 可以为我创建表格,然后我将其改回原样,一切正常!
问题是您的迁移从未得到应用,实际上您在服务器上 运行 migrate
时应该遇到同样的错误。这里的问题是您有以下行:
'parent': forms.Select(attrs={'class': 'form-control mb-1'}, choices=[(m, m) for m in MenuOption.objects.all()]),
这一行自然地查询了数据库,这有什么问题吗?此行在 class 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