Docker部署Django+PostgreSQL服务无法执行迁移问题求助
解决Django迁移报错:relation "accounts_customuser" does not exist
问题背景
基于《Django for Professionals》的书店项目扩展开发,使用Docker+PostgreSQL环境,执行数据库迁移或创建超级用户时,出现relation "accounts_customuser" does not exist错误。数据库中仅存在django_migrations表,无法完成迁移。
当前环境配置
requirements.txt
asgiref==3.5.2 Django==4.0.4 psycopg2-binary==2.9.3 sqlparse==0.4.2 django-crispy-forms==1.14.0 crispy-bootstrap5==0.6 django-allauth==0.50.0
Dockerfile
FROM python:3.8-slim-bullseye # set environment variables ENV PIP_DISABLE_PIP_VERSION_CHECK 1 ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 # Set working directory WORKDIR /code # Installing python dependencies COPY ./requirements.txt . RUN pip install -r requirements.txt
docker-compose.yml
# Mentioning which format of dockerfile version: "3.9" # services or nicknamed the container services: # web service for the web web: # you should use the --build flag for every node package added build: . # Add additional commands for webpack to 'watch for changes and bundle it to production' command: python manage.py runserver 0.0.0.0:8000 volumes: - type: bind source: . target: /code ports: - "8000:8000" depends_on: - db environment: - "DJANGO_SECRET_KEY=django-insecure-m#x2vcrd_2un!9b4la%^)ou&hcib&nc9fvqn0s23z%i1e5))6&" - "DJANGO_DEBUG=True" # postgreSQL database server being constructed alongside db: image: postgres:13 volumes: - postgres_data:/var/lib/postgresql/data/ environment: - "POSTGRES_HOST_AUTH_METHOD=trust" # Volumes set up volumes: postgres_data:
核心报错信息
执行迁移时的错误:
P:\StockWhiz> docker compose exec web python manage.py migrate Operations to perform: Apply all migrations: account, admin, auth, contenttypes, sessions, sites, socialaccount Running migrations: Applying account.0001_initial...Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 89, in _execute return self.cursor.execute(sql, params) psycopg2.errors.UndefinedTable: relation "accounts_customuser" does not exist ... django.db.utils.ProgrammingError: relation "accounts_customuser" does not exist
Docker控制台错误:
stockwhiz-db-1 | 2022-11-04 13:36:40.562 UTC [36] ERROR: relation "accounts_customuser" does not exist ", "accounts_customuser"."password", "accounts_customuser"."last_login", "accounts_customuser"."is_superuser", "accounts_customuser"."username", "accounts_customuser"."first_name", "accounts_customuser"."last_name", "accounts_customuser"."email", "accounts_customuser"."is_staff", "accounts_customuser"."is_active", "accounts_customuser"."date_joined" FROM "accounts_customuser" WHERE "accounts_customuser"."username" = 'admin' LIMIT 21 stockwhiz-db-1 | 2022-11-04 13:38:26.019 UTC [41] ERROR: relation "accounts_customuser" does not existstockwhiz-db-1 | 2022-11-04 13:38:26.019 UTC [41] STATEMENT: ALTER TABLE "account_emailaddress" ADD CONSTRAINT "account_emailaddress_user_id_2c513194_fk_accounts_customuser_id" FOREIGN KEY ("user_id") REFERENCES "accounts_customuser" ("id") DEFERRABLE INITIALLY DEFERRED
报错原因
django-allauth的account迁移依赖自定义用户表accounts_customuser,但默认执行migrate时,Django会按字母顺序执行迁移,account的迁移先于accounts(自定义用户app)执行,导致找不到依赖表。此外,容器启动时runserver会触发Django初始化,尝试访问用户表,进一步破坏迁移状态。
解决方案步骤
1. 清理异常数据库状态
停止并删除现有容器和数据库卷,重置数据库:
docker compose down -v
2. 临时调整容器启动命令
修改docker-compose.yml中web服务的command,避免启动时自动触发Django初始化:
command: sleep infinity
3. 启动容器并手动执行迁移
启动容器:
docker compose up -d
先迁移自定义用户所在的accounts app,确保用户表先创建:
docker compose exec web python manage.py migrate accounts
再执行所有剩余迁移:
docker compose exec web python manage.py migrate
4. 恢复启动命令并创建超级用户
将docker-compose.yml中web服务的command恢复为:
command: python manage.py runserver 0.0.0.0:8000
重启容器:
docker compose restart web
创建超级用户:
docker compose exec web python manage.py createsuperuser
5. 关键配置检查
确认项目settings.py中已正确设置自定义用户模型:
AUTH_USER_MODEL = 'accounts.CustomUser'
确保accounts app在INSTALLED_APPS中,且位于django-allauth相关app之前:
INSTALLED_APPS = [ # ... 'accounts', 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', # ... ]
内容的提问来源于stack exchange,提问作者Savakar Rohan
相关产品推荐
相关产品推荐

