Heroku 和 Django - 关系不存在

Posted

技术标签:

【中文标题】Heroku 和 Django - 关系不存在【英文标题】:Heroku and Django - relation does not exist 【发布时间】:2018-07-16 14:50:02 【问题描述】:

我在 Heroku 上启动了一个应用程序,运行 Django 2.0.1,并提供了 Postgres 资源。我的 Procfile,经过几次迭代,看起来像这样:

过程文件

release: python manage.py migrate --noinput
web: gunicorn app.wsgi --log-file -

当我从 GitHub 部署时(不是从命令行部署...我之前没有从命令行部署,因为 GitHub 部署工作对我来说至关重要)一切正常。迁移按应有的方式运行,并且日志确认迁移时没有错误。事实上,部署到 Heroku 完全没有错误。

当我启动网站(强制它进入调试模式以查看错误)时,我在首页收到此错误:

ProgrammingError at /
relation "posts_post" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "posts_post" INNER JOIN "u...

尝试加载管理站点时存在类似错误:

ProgrammingError at /admin/login/
relation "django_site" does not exist
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...
                                                             ^

我按照Django: relation "django_site" does not exist 中的建议先迁移sites,但没有任何效果。

另外,this issue on GitHub is related,但没有帮助。

为了澄清,该应用程序是可访问的。 Heroku 部署不会失败。该应用程序只是出错了。

追溯(部分)

Environment:


Request Method: GET
Request URL: https://somesite.herokuapp.com/

Django Version: 2.0.1
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'django.contrib.humanize',
 'accounts.apps.AccountsConfig',
 'analytics.apps.AnalyticsConfig',
 'api_v1.apps.ApiV1Config',
 'api_v2.apps.ApiV2Config',
 'meta.apps.MetaConfig',
 'posts.apps.PostsConfig',
 'users.apps.UsersConfig',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'rest_framework']
Installed 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',
 'whitenoise.middleware.WhiteNoiseMiddleware']



Traceback:

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

The above exception (relation "posts_post" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "posts_post" INNER JOIN "u...
                                          ^

为什么会出现这些错误?我该怎么做才能让我在 Heroku 上部署的 Django 正常工作?

附加信息:

settings.py(为了安全而隐藏)

ABSOLUTE_URL_OVERRIDES=
ACCOUNT_AUTHENTICATION_METHOD='username_email'
ACCOUNT_EMAIL_REQUIRED=True
ACCOUNT_UNIQUE_EMAIL=True
ADMINS=[]
ALLOWED_HOSTS=['somesite.herokuapp.com']
APPEND_SLASH=True
AUTHENTICATION_BACKENDS=('django.contrib.auth.backends.ModelBackend',
 'allauth.account.auth_backends.AuthenticationBackend')
AUTH_PASSWORD_VALIDATORS='********************'
AUTH_USER_MODEL='users.User'
BASE_DIR='/app'
BLEACH_ALLOWED_ATTRIBUTES=...
BLEACH_ALLOWED_PROTOCOLS=[...]
BLEACH_ALLOWED_TAGS=[...]
CACHES='default': 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
CACHE_MIDDLEWARE_ALIAS='default'
CACHE_MIDDLEWARE_KEY_PREFIX='********************'
CACHE_MIDDLEWARE_SECONDS=600
CSRF_COOKIE_AGE=31449600
CSRF_COOKIE_DOMAIN=None
CSRF_COOKIE_HTTPONLY=False
CSRF_COOKIE_NAME='csrftoken'
CSRF_COOKIE_PATH='/'
CSRF_COOKIE_SECURE=False
CSRF_FAILURE_VIEW='django.views.csrf.csrf_failure'
CSRF_HEADER_NAME='HTTP_X_CSRFTOKEN'
CSRF_TRUSTED_ORIGINS=[]
CSRF_USE_SESSIONS=False
DATABASES='default': 'ATOMIC_REQUESTS': False,
             'AUTOCOMMIT': True,
             'CONN_MAX_AGE': 0,
             'ENGINE': 'django.db.backends.postgresql_psycopg2',
             'HOST': 'ec2-12-34-56-78.compute-1.amazonaws.com',
             'NAME': 'ddr3sshvtab',
             'OPTIONS': ,
             'PASSWORD': '********************',
             'PORT': 5432,
             'TEST': 'CHARSET': None,
                      'COLLATION': None,
                      'MIRROR': None,
                      'NAME': None,
             'TIME_ZONE': None,
             'USER': 'jabbathehutt'
DATABASE_ROUTERS=[]
DATA_UPLOAD_MAX_MEMORY_SIZE=2621440
DATA_UPLOAD_MAX_NUMBER_FIELDS=1000
DATETIME_FORMAT='N j, Y, P'
DATETIME_INPUT_FORMATS=['%Y-%m-%d %H:%M:%S',
 '%Y-%m-%d %H:%M:%S.%f',
 '%Y-%m-%d %H:%M',
 '%Y-%m-%d',
 '%m/%d/%Y %H:%M:%S',
 '%m/%d/%Y %H:%M:%S.%f',
 '%m/%d/%Y %H:%M',
 '%m/%d/%Y',
 '%m/%d/%y %H:%M:%S',
 '%m/%d/%y %H:%M:%S.%f',
 '%m/%d/%y %H:%M',
 '%m/%d/%y']
DATE_FORMAT='N j, Y'
DATE_INPUT_FORMATS=['%Y-%m-%d',
 '%m/%d/%Y',
 '%m/%d/%y',
 '%b %d %Y',
 '%b %d, %Y',
 '%d %b %Y',
 '%d %b, %Y',
 '%B %d %Y',
 '%B %d, %Y',
 '%d %B %Y',
 '%d %B, %Y']
DEBUG=True
DEBUG_PROPAGATE_EXCEPTIONS=False
DECIMAL_SEPARATOR='.'
DEFAULT_CHARSET='utf-8'
DEFAULT_CONTENT_TYPE='text/html'
DEFAULT_EXCEPTION_REPORTER_FILTER='django.views.debug.SafeExceptionReporterFilter'
DEFAULT_FILE_STORAGE='django.core.files.storage.FileSystemStorage'
DEFAULT_FROM_EMAIL='webmaster@localhost'
DEFAULT_INDEX_TABLESPACE=''
DEFAULT_TABLESPACE=''
DISALLOWED_USER_AGENTS=[]
EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST='smtp.mailgun.org'
EMAIL_HOST_PASSWORD='********************'
EMAIL_HOST_USER='postmaster@mg.mailgun.com'
EMAIL_PORT=587
EMAIL_SSL_CERTFILE=None
EMAIL_SSL_KEYFILE='********************'
EMAIL_SUBJECT_PREFIX='[Django] '
EMAIL_TIMEOUT=None
EMAIL_USE_LOCALTIME=False
EMAIL_USE_SSL=False
EMAIL_USE_TLS=True
ERROR_404_VIDEO_EMBED_LINK='https://www.youtube.com/embed/GSRG0TqxLWc'
ERROR_FEEDBACK_FORM_LINK='https://short.url/here'
FILE_CHARSET='utf-8'
FILE_UPLOAD_DIRECTORY_PERMISSIONS=None
FILE_UPLOAD_HANDLERS=
['django.core.files.uploadhandler.MemoryFileUploadHandler',
 'django.core.files.uploadhandler.TemporaryFileUploadHandler']
FILE_UPLOAD_MAX_MEMORY_SIZE=2621440
FILE_UPLOAD_PERMISSIONS=None
FILE_UPLOAD_TEMP_DIR=None
FIRST_DAY_OF_WEEK=0
FIXTURE_DIRS=[]
FORCE_SCRIPT_NAME=None
FORMAT_MODULE_PATH=None
FORM_RENDERER='django.forms.renderers.DjangoTemplates'
GOOGLE_ANALYTICS_TRACKING_ID='UA-1111111-8'
IGNORABLE_404_URLS=[]
INSTALLED_APPS=['django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'django.contrib.humanize',
 'accounts.apps.AccountsConfig',
 'analytics.apps.AnalyticsConfig',
 'api_v1.apps.ApiV1Config',
 'api_v2.apps.ApiV2Config',
 'meta.apps.MetaConfig',
 'posts.apps.PostsConfig',
 'users.apps.UsersConfig',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'rest_framework']
INTERNAL_IPS=[]
LANGUAGES=[...]
LANGUAGES_BIDI=['he', 'ar', 'fa', 'ur']
LANGUAGE_CODE='en-us'
LANGUAGE_COOKIE_AGE=None
LANGUAGE_COOKIE_DOMAIN=None
LANGUAGE_COOKIE_NAME='django_language'
LANGUAGE_COOKIE_PATH='/'
LOCALE_PATHS=[]
LOGGING=
LOGGING_CONFIG='logging.config.dictConfig'
LOGIN_REDIRECT_URL='/accounts/profile/'
LOGIN_URL='/accounts/login/'
LOGOUT_REDIRECT_URL=None
MANAGERS=[]
MEDIA_ROOT=''
MEDIA_URL=''
MESSAGE_STORAGE='django.contrib.messages.storage.fallback.FallbackStorage'
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',
 'whitenoise.middleware.WhiteNoiseMiddleware']
MIGRATION_MODULES=
MONTH_DAY_FORMAT='F j'
NUMBER_GROUPING=0
PASSWORD_HASHERS='********************'
PASSWORD_RESET_TIMEOUT_DAYS='********************'
PREPEND_WWW=False
REST_FRAMEWORK='DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticatedOrReadOnly',),
 'PAGE_SIZE': 50
ROOT_URLCONF='app.urls'
SECRET_KEY='********************'
SECURE_BROWSER_XSS_FILTER=False
SECURE_CONTENT_TYPE_NOSNIFF=False
SECURE_HSTS_INCLUDE_SUBDOMAINS=False
SECURE_HSTS_PRELOAD=False
SECURE_HSTS_SECONDS=0
SECURE_PROXY_SSL_HEADER=None
SECURE_REDIRECT_EXEMPT=[]
SECURE_SSL_HOST=None
SECURE_SSL_REDIRECT=False
SERVER_EMAIL='root@localhost'
SESSION_CACHE_ALIAS='default'
SESSION_COOKIE_AGE=1209600
SESSION_COOKIE_DOMAIN=None
SESSION_COOKIE_HTTPONLY=True
SESSION_COOKIE_NAME='sessionid'
SESSION_COOKIE_PATH='/'
SESSION_COOKIE_SECURE=False
SESSION_ENGINE='django.contrib.sessions.backends.db'
SESSION_EXPIRE_AT_BROWSER_CLOSE=False
SESSION_FILE_PATH=None
SESSION_SAVE_EVERY_REQUEST=False
SESSION_SERIALIZER='django.contrib.sessions.serializers.JSONSerializer'
SETTINGS_MODULE='app.settings.production'
SHORT_DATETIME_FORMAT='m/d/Y P'
SHORT_DATE_FORMAT='m/d/Y'
SIGNING_BACKEND='django.core.signing.TimestampSigner'
SILENCED_SYSTEM_CHECKS=[]
SITE_ID=1
SITE_NAME='Super Cool Site'
SITE_TAGLINE="Super Cool Tagline."
STATICFILES_DIRS=[]
STATICFILES_FINDERS=['django.contrib.staticfiles.finders.FileSystemFinder',
 'django.contrib.staticfiles.finders.AppDirectoriesFinder']
STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage'
STATIC_ROOT='/app/static'
STATIC_URL='/static/'
TEMPLATES=['APP_DIRS': True,
  'BACKEND': 'django.template.backends.django.DjangoTemplates',
  'DIRS': ['/app/templates', '/app/templates/allauth', '/app/templates/errors'],
  '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',
                                     'quest.context_processors.template_constants']]
TEST_NON_SERIALIZED_APPS=[]
TEST_RUNNER='django.test.runner.DiscoverRunner'
THOUSAND_SEPARATOR=','
TIME_FORMAT='P'
TIME_INPUT_FORMATS=['%H:%M:%S', '%H:%M:%S.%f', '%H:%M']
TIME_ZONE='UTC'
USE_ETAGS=False
USE_I18N=True
USE_L10N=True
USE_THOUSAND_SEPARATOR=False
USE_TZ=False
USE_X_FORWARDED_HOST=False
USE_X_FORWARDED_PORT=False
VERSION='1.0.0-alpha.2'
WSGI_APPLICATION='app.wsgi.application'
X_FRAME_OPTIONS='SAMEORIGIN'
YEAR_MONTH_FORMAT='F Y'

【问题讨论】:

显示您的数据库设置。你在使用 Heroku Postgres 插件吗? @DanielRoseman 添加了 settings.py sn-p 我也有同样的问题。每个迁移都在本地工作。但是,一旦我尝试在 heroku 上迁移某些东西,它就会由于我的“xyz_user”关系而失败。部署也有效。 你们是如何解决这个问题的? 【参考方案1】:

尝试运行

$Heroku run python manage.py migrate

在您的终端中。

【讨论】:

以上是关于Heroku 和 Django - 关系不存在的主要内容,如果未能解决你的问题,请参考以下文章

heroku:关系“auth_group”不存在

关系不存在 - Django & Postgres

Errno 2文件b'不存在:在heroku django中

将预先存在的 django git repo 部署到 heroku

Heroku postgres 关系“information_schema.session_state”不存在

为啥 Heroku 找不到我的 Django 模板?