質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.41%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1065閲覧

herokuにdeployしたDjangoアプリケーションのデータベースがmigrateできない

shirachin1015

総合スコア6

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Heroku

HerokuはHeroku社が開発と運営を行っているPaaSの名称です。RubyやNode.js、Python、そしてJVMベース(Java、Scala、Clojureなど)の複数のプログラミング言語をサポートしている。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2023/02/12 05:08

問題点

herokuにdeployしたDjangoアプリケーションのデータベースがmigrateできない

詳細

ローカルでは、問題なくmigrateできるアプリがherokuにdeployするとうまくmigrateが動作しなくなる。

具体的な状況は、Djangoでデータベースを使用しない簡易的なアプリケーションを作成後、ネットの様々な情報からsetting.pyなどを変更しherokuにdeployしたが、createsuperuserが作成出来ない。migrate自体にはOKの文字が出るが、showmigrationsでは[x]が1つもつかない。

発生している問題・エラーメッセージ

  • python manage.py createsuperuser 実行時
(.env) ... % heroku run python manage.py createsuperuser Running python manage.py createsuperuser on ⬢ serene-fortress-94071... up, run.9498 (Basic) You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. Traceback (most recent call last): File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute return self.cursor.execute(sql, params) File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 357, in execute return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such table: auth_user The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/app/manage.py", line 22, in <module> main() File "/app/manage.py", line 18, in main execute_from_command_line(sys.argv) File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line utility.execute() File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv self.execute(*args, **cmd_options) File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 88, in execute return super().execute(*args, **options) File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute output = self.handle(*args, **options) File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 109, in handle default_username = get_default_username(database=database) ... return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table: auth_user

  • python manage.py migrate 実行時
(.env) ... % heroku run python manage.py migrate Running python manage.py migrate on ⬢ serene-fortress-94071... up, run.7191 (Basic) Operations to perform: Apply all migrations: admin, auth, 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 admin.0003_logentry_add_action_flag_choices... 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 auth.0009_alter_user_last_name_max_length... OK Applying auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying auth.0012_alter_user_first_name_max_length... OK Applying sessions.0001_initial... OK

  • python manage.py showmigrations 実行時
(.env) ... % heroku run python manage.py showmigrations Running python manage.py showmigrations on ⬢ serene-fortress-94071... up, run.5713 (Basic) admin [ ] 0001_initial [ ] 0002_logentry_remove_auto_add [ ] 0003_logentry_add_action_flag_choices app (no migrations) 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 [ ] 0009_alter_user_last_name_max_length [ ] 0010_alter_group_name_max_length [ ] 0011_update_proxy_permissions [ ] 0012_alter_user_first_name_max_length contenttypes [ ] 0001_initial [ ] 0002_remove_content_type_name sessions [ ] 0001_initial

該当のソースコード

python3

1""" 2Django settings for AutoPost project. 3 4Generated by 'django-admin startproject' using Django 4.1.6. 5 6For more information on this file, see 7https://docs.djangoproject.com/en/4.1/topics/settings/ 8 9For the full list of settings and their values, see 10https://docs.djangoproject.com/en/4.1/ref/settings/ 11""" 12 13from pathlib import Path 14import dj_database_url 15import os 16 17# Build paths inside the project like this: BASE_DIR / 'subdir'. 18BASE_DIR = Path(__file__).resolve().parent.parent 19# BASE_DIR = dev/APapp/AutoPost 20 21ALLOWED_HOSTS = ['.herokuapp.com'] 22 23 24# Application definition 25 26INSTALLED_APPS = [ 27 'django.contrib.admin', 28 'django.contrib.auth', 29 'django.contrib.contenttypes', 30 'django.contrib.sessions', 31 'django.contrib.messages', 32 'django.contrib.staticfiles', 33 'app', 34] 35 36MIDDLEWARE = [ 37 'django.middleware.security.SecurityMiddleware', 38 'django.contrib.sessions.middleware.SessionMiddleware', 39 'django.middleware.common.CommonMiddleware', 40 'django.middleware.csrf.CsrfViewMiddleware', 41 'django.contrib.auth.middleware.AuthenticationMiddleware', 42 'django.contrib.messages.middleware.MessageMiddleware', 43 'django.middleware.clickjacking.XFrameOptionsMiddleware', 44] 45 46ROOT_URLCONF = 'AutoPost.urls' 47 48TEMPLATES = [ 49 { 50 'BACKEND': 'django.template.backends.django.DjangoTemplates', 51 'DIRS': [ 52 BASE_DIR / 'app' / 'templates' 53 ], 54 'APP_DIRS': True, 55 'OPTIONS': { 56 'context_processors': [ 57 'django.template.context_processors.debug', 58 'django.template.context_processors.request', 59 'django.contrib.auth.context_processors.auth', 60 'django.contrib.messages.context_processors.messages', 61 ], 62 }, 63 }, 64] 65 66WSGI_APPLICATION = 'AutoPost.wsgi.application' 67 68 69# Database 70# https://docs.djangoproject.com/en/4.1/ref/settings/#databases 71 72DATABASES = { 73 'default': { 74 'ENGINE': 'django.db.backends.postgresql_psycopg2', 75 'NAME': 'name', 76 'USER': 'user', 77 'PASSWORD': '', 78 'HOST': 'host', 79 'PORT': '', 80 } 81} 82 83 84# Password validation 85# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 86 87AUTH_PASSWORD_VALIDATORS = [ 88 { 89 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 90 }, 91 { 92 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 93 }, 94 { 95 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 96 }, 97 { 98 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 99 }, 100] 101 102 103# Internationalization 104# https://docs.djangoproject.com/en/4.1/topics/i18n/ 105 106LANGUAGE_CODE = 'ja' 107 108TIME_ZONE = 'Asia/Tokyo' 109 110USE_I18N = True 111 112USE_TZ = True 113 114 115# Static files (CSS, JavaScript, Images) 116# https://docs.djangoproject.com/en/4.1/howto/static-files/ 117 118STATIC_URL = 'static/' 119STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') 120 121# Default primary key field type 122# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 123 124DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 125 126db_from_env = dj_database_url.config(conn_max_age=600, ssl_require=True) 127DATABASES['default'].update(db_from_env) 128 129DEBUG = False 130 131try: 132 # 存在する場合、ローカルの設定読み込み 133 from .local_settings import * 134except ImportError: 135 pass 136 137if not DEBUG: 138 SECRET_KEY = os.environ['SECRET_KEY'] 139 import django_heroku #追加 140 django_heroku.settings(locals())

試したこと

https://devcenter.heroku.com/ja/articles/django-app-configuration

↑のサイトを読みましたが、間違っているところに気づくことができませんでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

DjangoやHerokuには何も悪いことはなく、gitが悪さをしていたみたいでした。

heroku run ls ./AutoPost/

を実行すると、ローカル用の設定ファイルである「local_settings.py」が入っており、gitのキャッシュにファイルが残っており、それを永遠にpushし続けていたことが原因でした。

git rm -r --cached .

でキャッシュを削除した後に、add & commit & pushしたらlocal_settings.pyは無事削除され、データベースを正常に作成することができました。

投稿2023/02/15 08:05

shirachin1015

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.41%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問