0
0

More than 3 years have passed since last update.

rails アプリをherokuに上げるまでの道のり プログラミング学習42日目

Posted at

DBはpgがやりやすい

自身の開発環境のdbはmysql2でherokuつまり本番環境のデフォルトのDBはpgらしい。herokuのDBをmysql2に変更することもできるっぽいが、今回は自身の本番環境をpgにします!

エラーの嵐

$git add .
$ git commit -m "change DB adapter to PG"
$ git push heroku

とherokuのプッシュしようとするととてつもない長さのエラーが出てしまいました。

エラーの原因はいくつかありました。
1.database.ymlの中身がmysql2を示していた
訂正後

herokuのsettingのものと合わせます(以下画像)
スクリーンショット 2019-11-20 18.12.57.png


default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  socket: /tmp/mysql.sock

development:
  <<: *default
  database: app_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: app_test

================================================================

production:
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: <%= ENV.fetch('DATABASE_URL') { 'localhost' } %>

2.herokuのDBをmysqlに変更していたのに、本番環境をpgにしていた
3.DBにusers emailが重複していた

ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR:  column "email" of relation "users" already exists

これはgem 'devise'をちゃんと理解せず、user modelも別に作っていたことが原因だと考えられます。実際にdb/migrate/add_usersを消したらadd, commit ,pushしたらherokuで開くことができました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0