Hey All,
I’ve run into an error that makes me believe my customuser model is not migrating.
I’m using Docker to run my Django app, PostgreSQL db, RabbitMQ broker, and Celery worker.
(string_v3) PS C:\Users\steve\Desktop\Coding\string_v3> docker-compose up -d --build
The app starts fine, but when I go to login, I get an error. The output from the logs is here:
Creating string_v3_db_1 ... done
Creating string_v3_web_1 ... done
Creating string_v3_rabbitmq_1 ... done
Creating string_v3_celeryworker_1 ... done
(string_v3) PS C:\Users\steve\Desktop\Coding\string_v3> docker-compose logs
Attaching to string_v3_celeryworker_1, string_v3_rabbitmq_1, string_v3_web_1, string_v3_db_1
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2021-10-03 12:51:22.015 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2021-10-03 12:51:22.015 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2021-10-03 12:51:22.023 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2021-10-03 12:51:22.041 UTC [26] LOG: database system was shut down at 2021-09-25 18:03:18 UTC
db_1 | 2021-10-03 12:51:22.058 UTC [1] LOG: database system is ready to accept connections
db_1 | 2021-10-03 12:53:36.671 UTC [36] ERROR: relation "users_customuser" does not exist at character 387
Abbreviating some of the logs for brevity …
_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
web_1 | return self.cursor.execute(sql, params)
web_1 | django.db.utils.ProgrammingError: relation "users_customuser" does not exist
web_1 | LINE 1: SELECT (1) AS "a" FROM "users_customuser" WHERE "users_custo...
Most of what I could find suggested to re-run migrations. I shut down and run migrations:
(string_v3) PS C:\Users\steve\Desktop\Coding\string_v3> docker-compose down
Stopping string_v3_celeryworker_1 ... done
Stopping string_v3_rabbitmq_1 ... done
Stopping string_v3_web_1 ... done
Stopping string_v3_db_1 ... done
Removing string_v3_celeryworker_1 ... done
Removing string_v3_rabbitmq_1 ... done
Removing string_v3_web_1 ... done
Removing string_v3_db_1 ... done
Removing network string_v3_default
(string_v3) PS C:\Users\steve\Desktop\Coding\string_v3> docker-compose exec web python manage.py makemigrations
(string_v3) PS C:\Users\steve\Desktop\Coding\string_v3> docker-compose exec web python manage.py migrate
This doesn’t seem to solve my problem.
docker-comose.yml
version: '3.8'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
depends_on:
- db
db:
image: postgres:11
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
rabbitmq:
image: rabbitmq:3.8.18-management
hostname: rabbitmq
ports:
- 15672:15672
- 5672:5672
expose:
- 15672
depends_on:
- web
celeryworker:
build: .
depends_on:
- rabbitmq
command: celery -A string_project worker -l info --pool=solo
volumes:
postgres_data:
users/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUser(AbstractUser):
company=models.CharField(
max_length=100,
null=False, # false means value is needed in db
blank=False, #false means value is needed in forms
editable=True,
)
settings.py
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'removed'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['*'] #added the star here based on recommendation from Docker. Needs to be removed for production.
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'users.apps.UsersConfig',
'pages.apps.PagesConfig',
'applications.apps.ApplicationsConfig'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'string_project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'string_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS=(str(BASE_DIR.joinpath('static')),)
STATIC_ROOT= str(BASE_DIR.joinpath('staticfiles'))
STATFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
AUTH_USER_MODEL='users.CustomUser'
LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'
CRISPY_TEMPLATE_PACK = 'bootstrap4'
# Email Config
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'removed'
EMAIL_HOST_PASSWORD = 'removed'
# Celery Config
CELERY_BROKER_URL = 'amqp://rabbitmq:5672/'
#CELERY_RESULT_BACKEND = 'amqp'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_AMQP_TASK_RESULT_EXPIRES = 1000
CELERY_ACKS_LATE=False
CELERY_TASK_TRACK_STARTED=True
CELERY_SEND_TASK_EVENTS=True
I don’t have anything I’m concerned about saving in any of my databases.
Does anyone see anything that would be causing my error?
Steve