heroku django.db.utils.OperationalError:no such table:auth_user & Warning:Root没有设置database_url环境变量,因此没有设置数据库

w8biq8rn  于 2024-07-24  发布在  Go
关注(0)|答案(1)|浏览(210)

我是一名从事全栈开发的学生。
我正在尝试将我的Django应用程序与sqlite3部署到Heroku。
我正在尝试创建一个超级用户:heroku run python manage.py
它给出错误django.db.utils.Operatorerror:no such table:auth_user
我已经运行了代码heroku run python.manage.py migrate,它都显示OK。
但是当我运行createsuperuser时,它显示了21个未应用的迁移。
我也运行python manage.py migrate,它没有说migrate.You have 21 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, recipe, sessions, user. Run 'python manage.py migrate' to apply them.
不知道是什么错误以及如何修复它。
当我在settings.py下面的最后3行添加数据库配置时,我的代码显示警告

  1. """
  2. Django settings for recipe_project project.
  3. Generated by 'django-admin startproject' using Django 4.2.7.
  4. For more information on this file, see
  5. https://docs.djangoproject.com/en/4.2/topics/settings/
  6. For the full list of settings and their values, see
  7. https://docs.djangoproject.com/en/4.2/ref/settings/
  8. """
  9. import os
  10. from pathlib import Path
  11. # Build paths inside the project like this: BASE_DIR / 'subdir'.
  12. BASE_DIR = Path(__file__).resolve().parent.parent
  13. # Quick-start development settings - unsuitable for production
  14. # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
  15. # SECURITY WARNING: keep the secret key used in production secret!
  16. SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY','django-insecure-ml49cp(e)=yakpevh4xz)3w)6xuq6kv7g&3^xf^)gr-n3&p#%9')
  17. # SECURITY WARNING: don't run with debug turned on in production!
  18. DEBUG = False
  19. ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
  20. # Application definition
  21. INSTALLED_APPS = [
  22. 'django.contrib.admin',
  23. 'django.contrib.auth',
  24. 'django.contrib.contenttypes',
  25. 'django.contrib.sessions',
  26. 'django.contrib.messages',
  27. 'django.contrib.staticfiles',
  28. # recipe_project-related apps
  29. 'recipe',
  30. 'user',
  31. ]
  32. MIDDLEWARE = [
  33. 'django.middleware.security.SecurityMiddleware',
  34. 'whitenoise.middleware.WhiteNoiseMiddleware',
  35. 'django.contrib.sessions.middleware.SessionMiddleware',
  36. 'django.middleware.common.CommonMiddleware',
  37. 'django.middleware.csrf.CsrfViewMiddleware',
  38. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  39. 'django.contrib.messages.middleware.MessageMiddleware',
  40. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  41. ]
  42. ROOT_URLCONF = 'recipe_project.urls'
  43. TEMPLATES = [
  44. {
  45. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  46. 'DIRS': [BASE_DIR / 'templates'],
  47. 'APP_DIRS': True,
  48. 'OPTIONS': {
  49. 'context_processors': [
  50. 'django.template.context_processors.debug',
  51. 'django.template.context_processors.request',
  52. 'django.contrib.auth.context_processors.auth',
  53. 'django.contrib.messages.context_processors.messages',
  54. ],
  55. },
  56. },
  57. ]
  58. WSGI_APPLICATION = 'recipe_project.wsgi.application'
  59. # Database
  60. # https://docs.djangoproject.com/en/4.2/ref/settings/#databases
  61. DATABASES = {
  62. 'default': {
  63. 'ENGINE': 'django.db.backends.sqlite3',
  64. 'NAME': BASE_DIR / 'db.sqlite3',
  65. }
  66. }
  67. # Password validation
  68. # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
  69. AUTH_PASSWORD_VALIDATORS = [
  70. {
  71. 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  72. },
  73. {
  74. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  75. },
  76. {
  77. 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  78. },
  79. {
  80. 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  81. },
  82. ]
  83. # Internationalization
  84. # https://docs.djangoproject.com/en/4.2/topics/i18n/
  85. LANGUAGE_CODE = 'en-us'
  86. TIME_ZONE = 'UTC'
  87. USE_I18N = True
  88. USE_TZ = True
  89. # Static files (CSS, JavaScript, Images)
  90. # https://docs.djangoproject.com/en/3.2/howto/static-files/
  91. STATIC_URL = '/static/'
  92. STATICFILES_DIRS=[
  93. ]
  94. # The absolute path to the directory where collectstatic will collect static files for deployment.
  95. MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  96. STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
  97. MEDIA_URL = '/media/'
  98. # Default primary key field type
  99. # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
  100. DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
  101. # Heroku: Update database configuration from $DATABASE_URL.
  102. import dj_database_url
  103. db_from_env = dj_database_url.config(conn_max_age=500)
  104. DATABASES['default'].update(db_from_env)

字符串
错误是:
根目录:没有设置DATABASE_URL环境变量,因此没有设置数据库
我已经谷歌试图解决,但无济于事。
请告知

hgncfbus

hgncfbus1#

Heroku有一个临时文件系统。你可以写它,也可以读它,但是内容会定期清除。在documentation中阅读更多。
您定义

  1. import dj_database_url
  2. db_from_env = dj_database_url.config(conn_max_age=500)
  3. DATABASES['default'].update(db_from_env)

字符串
这意味着它将尝试从环境变量中获取数据库信息。未设置:

  1. WARNING:root:No DATABASE_URL environment variable set, and so no databases setup


此外,您的设置文件定义了两个数据库(前者被覆盖):

  1. DATABASES = {
  2. 'default': {
  3. 'ENGINE': 'django.db.backends.sqlite3',
  4. 'NAME': BASE_DIR / 'db.sqlite3',
  5. }
  6. }
  7. ...
  8. DATABASES['default'].update(db_from_env)


我认为这是一个错误,应该得到修复。您的迁移无法应用,因为数据库无法访问/设置。使用Postgres via Heroku或其他外部数据库并相应地设置它。

展开查看全部

相关问题