Heroku中的Django部署:createsuperuser失败:django.db.utils.OperationalError:没有这样的表:auth_user
Posted
技术标签:
【中文标题】Heroku中的Django部署:createsuperuser失败:django.db.utils.OperationalError:没有这样的表:auth_user【英文标题】:Django deployment in Heroku: Fails in createsuperuser : django.db.utils.OperationalError: no such table: auth_user 【发布时间】:2018-01-18 20:42:04 【问题描述】:关于这个特定错误有很多 *** 问题,但我认为没有一个与我有类似的情况。我一直在关注 djangogirls heroku 部署教程,来自settings.py
中的数据库设置,确保我做得正确。
我在主机中启动并运行了应用程序。现在尝试访问管理员,它仍然有 auth_user 的操作错误。因此,我认为我可能只是在迁移时出错了。所以按照这些步骤我这样做了:
1.) python manage.py makemigrations
2.) python manage.py 迁移
3.) 混帐添加。 , git commit -m "-" , git push heroku master
4.) heroku 运行 python manage.py makemigrations
5.) heroku 运行 python manage.py 迁移
现在最后一步,这里一切都失败了,说还有东西要迁移。
6.) heroku 运行 python manage.py createsuperuser
我所做的步骤是他们提出并被接受的建议,但在我的情况下似乎不起作用。有什么想法吗?
git push heroku master
Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 584 bytes | 0 bytes/s, done.
Total 6 (delta 5), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing requirements with pip
remote:
remote: -----> $ python manage.py collectstatic --noinput
remote: 61 static files copied to '/tmp/build_82c26be81e84293089f1ac30a26227e2/hope/staticfiles', 24 post-processed.
remote:
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 162.1M
remote: -----> Launching...
remote: Released v23
remote: https://plexus-test.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/plexus-test.git
05985ed..ef721b3 master -> master
heroku run python manage.py makemigrations
System check identified some issues:
WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
blog.Post.created_date: (fields.W161) Fixed default value provided.
HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Migrations for 'blog':
blog/migrations/0004_auto_20170810_1400.py
- Alter field created_date on post
heroku run python manage.py migrate
System check identified some issues:
WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
blog.Post.created_date: (fields.W161) Fixed default value provided.
HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying blog.0001_initial... OK
Applying blog.0002_auto_20170808_1512... OK
Applying blog.0003_auto_20170810_1322... OK
Applying sessions.0001_initial... OK
heroku logs
2017-08-11T06:49:26.623403+00:00 app[api]: Starting process with
command `python manage.py migrate` by user
2017-08-11T06:49:38.089108+00:00 heroku[run.8604]: Awaiting client
2017-08-11T06:49:38.121068+00:00 heroku[run.8604]: Starting process with command `python manage.py migrate`
2017-08-11T06:49:38.345055+00:00 heroku[run.8604]: State changed from starting to up
2017-08-11T06:49:43.808801+00:00 heroku[run.8604]: Process exited with status 0
2017-08-11T06:49:43.823668+00:00 heroku[run.8604]: State changed from up to complete
heroku run python manage.py showmigrations
这里似乎已经错了。 Migrations 说它被应用了,但 showmigrations 说不然
admin
[ ] 0001_initial
[ ] 0002_logentry_remove_auto_add
auth
[ ] 0001_initial
[ ] 0002_alter_permission_name_max_length
[ ] 0003_alter_user_email_max_length
[ ] 0004_alter_user_username_opts
[ ] 0005_alter_user_last_login_null
[ ] 0006_require_contenttypes_0002
[ ] 0007_alter_validators_add_error_messages
[ ] 0008_alter_user_username_max_length
blog
[ ] 0001_initial
[ ] 0002_remove_post_created_date
contenttypes
[ ] 0001_initial
[ ] 0002_remove_content_type_name
sessions
[ ] 0001_initial
test_app
(no migrations)
heroku run python manage.py createsuperuser
System check identified some issues:
WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
blog.Post.created_date: (fields.W161) Fixed default value provided.
HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
You have 16 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, blog, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
return super(Command, self).execute(*args, **options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 96, in handle
default_username = get_default_username()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 148, in get_default_username
auth_app.User._default_manager.get(username=default_username)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 374, in get
num = len(clone)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 232, in __len__
self._fetch_all()
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 894, in execute_sql
raise original_exception
django.db.utils.OperationalError: no such table: auth_user
settings.py
"""
Django settings for hope project.
Generated by 'django-admin startproject' using Django 1.10.5.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
import dj_database_url
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '###'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = [
u'plexus-test.herokuapp.com',
u'127.0.0.1'
]
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'test_app',
'blog'
]
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',
]
ROOT_URLCONF = 'hope.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 = 'hope.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES =
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
try:
from .local_settings import *
except ImportError:
pass
# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
编辑:我在 virtualenv 上运行。
【问题讨论】:
【参考方案1】:最后,我重新进行了整个设置,一切顺利,没有任何问题。无论如何都将问题保留在这里,以防万一有人遇到同样的问题并且最终得到更好的回答。
【讨论】:
【参考方案2】:我遇到了同样的问题,在本地机器上一切正常,但是当我尝试创建超级用户时 heroku 给出了这个错误。
所以,它得到了修复,在 settings.py 中添加了以下内容:-
import django_heroku
django_heroku.settings(locals())
这会将您的本地设置和应用程序与 heroku 连接起来。
谢谢,希望对你有帮助。
【讨论】:
我阅读了大量其他答案,令人惊讶的是,这是唯一有效的方法。看起来 DATABASE_URL 需要某种配置才能与 Heroku 一起使用。 pypi.org/project/dj-database-url 这也可以。【参考方案3】:那里似乎说明了您的 Heroku 错误...
您有 16 个未应用的迁移。您的项目可能无法正常工作 直到您为应用程序应用迁移:admin、auth、blog、 内容类型,会话。运行“python manage.py migrate”以应用它们。
我只想跑……
>>> python manage.py migrate
【讨论】:
嗯,是的,就是这样。正如我上面所说,我相应地做了以下步骤......我一次又一次地按照错误状态进行迁移,但每次我进入 createsuperuser 步骤时,它都会显示相同的错误 所以你的数据库表需要在 createsuperuser 之前出现,你的表不在那里,这是问题所在,没有用户表就不能创建超级用户。 迁移命令不应该自动创建表吗?它确实说它“应用”了迁移。来自 admin、auth、contenttypes 和 session 以及我的模型。我想知道这些警告是否会阻碍从迁移中创建表。 我会尽量明确迁移...也许说 >>> python makemigrations auth than... >>> python migrate auth 如果这不起作用,我建议你运行在将模型或应用程序添加到项目之前,您的初始迁移和创建超级用户。所以在添加之前尝试运行这些迁移而不是添加你的博客。 "迁移命令不应该自动创建表吗?"是的,但是您的设置无法正常工作。如果可能,我肯定会删除数据库,而不是在添加博客或测试应用程序之前运行初始迁移。【参考方案4】:由于数据库结构的一些变化(可能是手动完成的),持久层和模型之间可能缺乏同步。如果是这种情况,您需要在继续之前清理并重置迁移。
https://www.techiediaries.com/how-to-reset-migrations-in-django-17-18-19-and-110/
我建议您确定这是否是您的情况。如果应用程序处于生产阶段,请非常小心!
【讨论】:
感谢此链接。我会试试这个并告诉你以上是关于Heroku中的Django部署:createsuperuser失败:django.db.utils.OperationalError:没有这样的表:auth_user的主要内容,如果未能解决你的问题,请参考以下文章
使用 docker 在 heroku 中部署 Django api rest
Django 'pip install django-heroku'(psycopg2) 错误阻止部署到 Heroku