Docker上的Django - 角色109中不存在关系“django_session”

时间:2017-02-15 12:03:09

标签: django docker docker-compose

我正在尝试使用Docker运行基于coockiecutter的Django项目,但是我遇到了这些错误:

完整输出:https://dpaste.de/eKF5

sudo docker-compose up
crm_postgres_1 is up-to-date
Starting crm_django_1
Starting crm_nginx_1
Attaching to crm_postgres_1, crm_django_1, crm_nginx_1
django_1    | Postgres is up - continuing...
postgres_1  | LOG:  database system was shut down at 2017-02-15 10:39:52 UTC
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
postgres_1  | LOG:  database system is ready to accept connections
postgres_1  | LOG:  autovacuum launcher started
postgres_1  | LOG:  received smart shutdown request
postgres_1  | LOG:  autovacuum launcher shutting down
postgres_1  | LOG:  shutting down
postgres_1  | LOG:  database system is shut down
postgres_1  | LOG:  database system was shut down at 2017-02-15 10:41:58 UTC
postgres_1  | LOG:  MultiXact member wraparound protections are now enabled
postgres_1  | LOG:  database system is ready to accept connections
postgres_1  | LOG:  autovacuum launcher started
postgres_1  | ERROR:  relation "django_session" does not exist at character 109
postgres_1  | STATEMENT:  SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."expire_date" > '2017-02-15T11:04:25.807267+00:00'::timestamptz AND 

我应该检查什么?我不能自己调试。

4 个答案:

答案 0 :(得分:1)

您需要运行迁移才能创建数据库表。

通常,您使用migrate命令执行此操作。

python manage.py migrate

在您的情况下,您需要在容器内运行迁移。 Cookiecutter-Django docs提供了如何执行此操作的说明:

docker-compose run django python manage.py migrate

答案 1 :(得分:0)

您也可以仅为会话应用运行迁移。

docker-compose -f production.yml(local.yml) run django python manage.py 
migrate sessions

答案 2 :(得分:0)

如果运行“ migrate”命令没有为您创建表,请检查django_migrations”表中名为“ sessions”的“ app”列中的条目 如果有条目,则将其删除(从django_migrations其中app ='sessions';删除),然后重新运行“ migrate”脚本。 同样的情况也适用于您可能正在使用的缺少数据库表的其他应用程序...迁移脚本将仅运行django_migrations表中未列出的脚本。

答案 3 :(得分:0)

对我有用的东西

首先,我执行了命令docker-compose build以创建图像,然后执行了docker-compose up

一旦django服务器启动,我就会打开另一个命令行,连接到容器(docker exec -it container_name bash)并在python manage.py migrate; python manage.py makemigrations内部执行。

顺便说一下,这些是我的Dockerfile和docker-compose.yml文件的内容:

FROM python:3.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN pip install --upgrade pip && pip install -r requirements.txt
COPY . /code/
version: '3'

services: 
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"