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を示していた
訂正後
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で開くことができました。