Python 芹菜在使用postgresql时没有这样的表

Python 芹菜在使用postgresql时没有这样的表,python,django,postgresql,celery,Python,Django,Postgresql,Celery,我现在正在用docker和芹菜生产我的django项目。 为此,我在网上发布了一篇博文 现在我无意中发现了一个我似乎无法纠正的错误 使用sqlite运行容器时,一切正常, 但使用postgresql,我会遇到以下错误: celery-beat_1 | [2020-03-20 14:32:00,052: INFO/MainProcess] Scheduler: Sending due task check_routers_online (Router.tasks.check_routers_on

我现在正在用docker和芹菜生产我的django项目。 为此,我在网上发布了一篇博文

现在我无意中发现了一个我似乎无法纠正的错误

使用sqlite运行容器时,一切正常, 但使用postgresql,我会遇到以下错误:

celery-beat_1  | [2020-03-20 14:32:00,052: INFO/MainProcess] Scheduler: Sending due task check_routers_online (Router.tasks.check_routers_online)
celery_1       | [2020-03-20 14:32:00,066: INFO/MainProcess] Received task: Router.tasks.check_routers_online[d4cece14-1d20-43ae-8712-1ad48ce79208]
celery_1       | [2020-03-20 14:32:00,085: ERROR/ForkPoolWorker-1] Task Router.tasks.check_routers_online[d4cece14-1d20-43ae-8712-1ad48ce79208] raised unexpected: OperationalError('no such table: Router_router')
celery_1       | Traceback (most recent call last):
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
celery_1       |     return self.cursor.execute(sql, params)
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
celery_1       |     return Database.Cursor.execute(self, query, params)
celery_1       | sqlite3.OperationalError: no such table: Router_router
celery_1       |
celery_1       | The above exception was the direct cause of the following exception:
celery_1       |
celery_1       | Traceback (most recent call last):
celery_1       |   File "/usr/local/lib/python3.7/site-packages/celery/app/trace.py", line 385, in trace_task
celery_1       |     R = retval = fun(*args, **kwargs)
celery_1       |   File "/usr/local/lib/python3.7/site-packages/celery/app/trace.py", line 650, in __protected_call__
celery_1       |     return self.run(*args, **kwargs)
celery_1       |   File "/home/app/web/Router/tasks.py", line 14, in check_routers_online
celery_1       |     for router in routers:
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 276, in __iter__
celery_1       |     self._fetch_all()
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1261, in _fetch_all
celery_1       |     self._result_cache = list(self._iterable_class(self))
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 57, in __iter__
celery_1       |     results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1151, in execute_sql
celery_1       |     cursor.execute(sql, params)
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
celery_1       |     return super().execute(sql, params)
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
celery_1       |     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
celery_1       |     return executor(sql, params, many, context)
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
celery_1       |     return self.cursor.execute(sql, params)
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
celery_1       |     raise dj_exc_value.with_traceback(traceback) from exc_value
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
celery_1       |     return self.cursor.execute(sql, params)
celery_1       |   File "/usr/local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 396, in execute
celery_1       |     return Database.Cursor.execute(self, query, params)
celery_1       | django.db.utils.OperationalError: no such table: Router_router
这是我的docker撰写文件:

version: '3.3'

services:
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=***
      - POSTGRES_PASSWORD=*****
      - POSTGRES_DB=***
  redis:
    image: "redis:alpine"
  web:
    container_name: inventurliste_app
    build: .
    command: gunicorn Inventur.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/web/staticfiles
    expose:
      - 8000
    env_file:
      - .env
  nginx:
    container_name: inventurliste_nginx
    build: ./nginx
    volumes:
      - static_volume:/home/app/web/staticfiles
    ports:
      - 1337:80
    depends_on:
      - web
      - db
  celery:
    build: .
    command: celery -A Inventur worker -l info
    depends_on:
      - redis
      - db
  celery-beat:
    build: .
    command: celery -A Inventur beat -l info
    depends_on:
      - redis
      - db

volumes:
  static_volume:
  postgres_data:
芹菜

import os
from celery import Celery


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Inventur.settings')

app = Celery('Inventur')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
my settings.py中的芹菜部分

CELERY_BROKER_URL = 'redis://redis:6379'
CELERY_RESULT_BACKEND = 'redis://redis:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

CELERY_BEAT_SCHEDULE = {
    'check_routers_online': {
        'task': 'Router.tasks.check_routers_online',
        'schedule': crontab()  # execute every minute
    },
    'refresh_all_ports': {
        'task': 'Switch.tasks.refresh_all_ports',
        'schedule': crontab(minute="0", hour="*/1")  # execute every hour
    }
}
DATABASES = {
    "default": {
        "ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"),
        "NAME": os.environ.get("SQL_DATABASE", os.path.join(BASE_DIR, "db.sqlite3")),
        "USER": os.environ.get("SQL_USER", "user"),
        "PASSWORD": os.environ.get("SQL_PASSWORD", "password"),
        "HOST": os.environ.get("SQL_HOST", "localhost"),
        "PORT": os.environ.get("SQL_PORT", "5432"),
    }, }
settings.py中的数据库部分

CELERY_BROKER_URL = 'redis://redis:6379'
CELERY_RESULT_BACKEND = 'redis://redis:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

CELERY_BEAT_SCHEDULE = {
    'check_routers_online': {
        'task': 'Router.tasks.check_routers_online',
        'schedule': crontab()  # execute every minute
    },
    'refresh_all_ports': {
        'task': 'Switch.tasks.refresh_all_ports',
        'schedule': crontab(minute="0", hour="*/1")  # execute every hour
    }
}
DATABASES = {
    "default": {
        "ENGINE": os.environ.get("SQL_ENGINE", "django.db.backends.sqlite3"),
        "NAME": os.environ.get("SQL_DATABASE", os.path.join(BASE_DIR, "db.sqlite3")),
        "USER": os.environ.get("SQL_USER", "user"),
        "PASSWORD": os.environ.get("SQL_PASSWORD", "password"),
        "HOST": os.environ.get("SQL_HOST", "localhost"),
        "PORT": os.environ.get("SQL_PORT", "5432"),
    }, }
环境署署长

makemigrations告诉我没有任何更改 而且也没用

我不能让它在sqlite中运行,所以我希望有人能帮助我

编辑:添加了设置和.env文件的数据库部分

哦,Django本身也很好用。postgres没有数据库问题
只是芹菜似乎有问题

看来我已经解决了

我还必须将.env文件添加到芹菜服务中

更新的docker compose:

version: '3.3'

services:
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=***
      - POSTGRES_PASSWORD=*****
      - POSTGRES_DB=***
  redis:
    image: "redis:alpine"
  web:
    container_name: inventurliste_app
    build: .
    command: gunicorn Inventur.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/web/staticfiles
    expose:
      - 8000
    env_file:
      - .env
  nginx:
    container_name: inventurliste_nginx
    build: ./nginx
    volumes:
      - static_volume:/home/app/web/staticfiles
    ports:
      - 1337:80
    depends_on:
      - web
      - db
  celery:
    build: .
    command: celery -A Inventur worker -l info
    depends_on:
      - redis
      - db
    env_file:     # here i added the .env
      - .env
  celery-beat:
    build: .
    command: celery -A Inventur beat -l info
    depends_on:
      - redis
      - db
    env_file:     # and here i had to add it too
      - .env

volumes:
  static_volume:
  postgres_data:

看来我已经修好了

我还必须将.env文件添加到芹菜服务中

更新的docker compose:

version: '3.3'

services:
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=***
      - POSTGRES_PASSWORD=*****
      - POSTGRES_DB=***
  redis:
    image: "redis:alpine"
  web:
    container_name: inventurliste_app
    build: .
    command: gunicorn Inventur.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/web/staticfiles
    expose:
      - 8000
    env_file:
      - .env
  nginx:
    container_name: inventurliste_nginx
    build: ./nginx
    volumes:
      - static_volume:/home/app/web/staticfiles
    ports:
      - 1337:80
    depends_on:
      - web
      - db
  celery:
    build: .
    command: celery -A Inventur worker -l info
    depends_on:
      - redis
      - db
    env_file:     # here i added the .env
      - .env
  celery-beat:
    build: .
    command: celery -A Inventur beat -l info
    depends_on:
      - redis
      - db
    env_file:     # and here i had to add it too
      - .env

volumes:
  static_volume:
  postgres_data:

您是否更新了Django数据库设置以使用Postgres?@DelenaMalan是的,我用这些设置编辑了我的线程。您是否更新了Django数据库设置以使用Postgres?@DelenaMalan是的,我用这些设置编辑了我的线程。