flask×Herokuアプリのコマンド
flask×HerokuでPostgresqlを使ったアプリ作るときのメモ
app 作成
$ heroku create Creating app... done, ⬢ XXX https://XXX.herokuapp.com/ | https://git.heroku.com/XXX.git
postgresql 作成
hobby-devで趣味用無料のDBが作れる。
$ heroku addons:create heroku-postgresql:hobby-dev $ heroku config
DATABASE_URLが入手できる。これをflaskのSQLALCHEMY_DATABASE_URIに設定する。
gitのremoteを付け替えるとき
$ git remote set-url heroku https://git.heroku.com/XXX.git $ git remote -v heroku https://git.heroku.com/XXX.git (fetch) heroku https://git.heroku.com/XXX.git (push)
デプロイ
pipenvの場合、Pipfile, Pipfile.lock, Procfileを用意しておく。
requirements.txtとrunitime.txtは自動で作られるのでいらない。
適宜、.gitignoreも設定しておく。
$ git add. $ git commit -m "text" $ git push heroku master
pipfile.lockがおかしいとき
以下のようなエラーでpush時にエラーが出る場合。
Your Pipfile.lock (7f5593) is out of date. Expected: (6c000a). Aborting deploy. ! Push rejected, failed to compile Python app. ! Push failed
$ pipenv update
db migrate
#herokuのbashで作業 $ heroku run bash $ export FLASK_APP=app.py $ flask db init # 古いmigrations/がアップロードされている場合エラーでるかも? # その場合 rm migrations/ -rfしてからリトライ $ flask db migrate $ flask db upgrade $ exit #テーブルができているか確認 $ flask pg:psql DATABASE=>\d #Ctrl+D