Search code examples
djangoherokudjango-modelsheroku-postgres

Heroku deployment of django project causing: Exception Type: ProgrammingError


I have tested this locally and it works fine. When I deploy to Heroku it deploys fine and serves the home page. Though When I try to sign up and create a new user I get a Internal 500 error and get the following error message.

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

Custom User Model

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'

Locally I have been testing using sqllite though I am using postgresql in heroku. I have set environment variables and it seems to be working upon deployment.

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

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

Solution

  • Did you remember to make migrations and migrate? heroku run python manage.py makemigrations and heroku run python manage.py migrate