Heroku部署django项目导致:异常类型:编程错误[重复]

tjrkku2a  于 2023-01-13  发布在  Go
关注(0)|答案(1)|浏览(113)
    • 此问题在此处已有答案**:

Django: ProgrammingError relation does not exists(2个答案)
Relation does not exist - Django & Postgres(3个答案)
Django on Heroku: relation does not exist(5个答案)
2天前关闭。
我已经在本地测试过了,它工作得很好。当我部署到Heroku时,它部署得很好,并提供主页。尽管当我试图注册并创建新用户时,我得到了内部500错误,并得到了以下错误信息。

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)

The above exception (relation "accounts_customuser" does not exist
LINE 1: SELECT 1 AS "a" FROM "accounts_customuser" WHERE "accounts_c...
                             ^
) was the direct cause of the following exception:
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch
    return handler(request, *args, **kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/views/generic/edit.py", line 184, in post
    return super().post(request, *args, **kwargs)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/views/generic/edit.py", line 152, in post
    if form.is_valid():
  File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/forms.py", line 205, in is_valid
    return self.is_bound and not self.errors
  File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/forms.py", line 200, in errors
    self.full_clean()
  File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/forms.py", line 439, in full_clean
    self._post_clean()
  File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/forms.py", line 129, in _post_clean
    super()._post_clean()
  File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/models.py", line 498, in _post_clean
    self.validate_unique()
  File "/app/.heroku/python/lib/python3.10/site-packages/django/forms/models.py", line 507, in validate_unique
    self.instance.validate_unique(exclude=exclude)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/base.py", line 1226, in validate_unique
    errors = self._perform_unique_checks(unique_checks)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/base.py", line 1336, in _perform_unique_checks
    if qs.exists():
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/query.py", line 1225, in exists
    return self.query.has_results(using=self.db)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/sql/query.py", line 592, in has_results
    return compiler.has_results()
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1363, in has_results
    return bool(self.execute_sql(SINGLE))
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1395, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 103, in execute
    return super().execute(sql, params)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /accounts/signup/
Exception Value: relation "accounts_customuser" does not exist
LINE 1: SELECT 1 AS "a" FROM "accounts_customuser" WHERE "accounts_c...

自定义用户模型

class CustomUser(AbstractUser):
    age = models.PositiveBigIntegerField(null=True, blank=True)
    business_name = models.CharField(null=True, blank=True, max_length=500)
    first_name = models.CharField(null=True, blank=True, max_length=500)
    last_name = models.CharField(null=True, blank=True, max_length=500)

Settings.py

AUTH_USER_MODEL = 'accounts.CustomUser'

虽然我在heroku中使用postgresql,但我一直在本地使用sqllite进行测试。我设置了环境变量,它似乎在部署时工作。

env = Env()
env.read_env()
...

DATABASES = {
    'default': env.dj_db_url("DATABASE_URL")
}
evrscar2

evrscar21#

您是否记得进行迁移和迁移?heroku run python manage.py makemigrationsheroku run python manage.py migrate

相关问题