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

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

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

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

481閲覧

herokuデプロイ後にmigrateができません

f--o

総合スコア10

Heroku

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

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/07/06 07:16

前提・実現したいこと

RailsアプリをHerokuを使ってデプロイしたいです。
git push heroku masterまでは実行できています。
開発環境でも問題なく動作しています。

dreamsというテーブルにopenedというカラムを持っているのですが、開発途中にboolean→integerに変更しました。(設計時の考え不足で、何度かbooleanとintegerを行ったり来たりしました。)

開発環境ではmysql、HerokuではpostgreSQLを利用しています。

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

heroku run rails db:migrateを行ったところエラーが発生しました。

(前略、成功している部分) rails aborted! StandardError: An error has occurred, this and all later migrations canceled: PG::DatatypeMismatch: ERROR: column "opened" cannot be cast automatically to type integer HINT: You might need to specify "USING opened::integer". : ALTER TABLE "dreams" ALTER COLUMN "opened" TYPE integer /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `async_exec' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:75:in `block (2 levels) in execute' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:74:in `block in execute' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:581:in `block (2 levels) in log' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:580:in `block in log' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/notifications/instrumenter.rb:23:in `instrument' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:571:in `log' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:73:in `execute' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql/schema_statements.rb:427:in `change_column' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:871:in `block in method_missing' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:840:in `block in say_with_time' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:840:in `say_with_time' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:860:in `method_missing' /app/db/migrate/20190702082636_change_data_opened_to_dreams.rb:3:in `change' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:814:in `exec_migration' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:798:in `block (2 levels) in migrate' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:797:in `block in migrate' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `with_connection' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:796:in `migrate' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:977:in `migrate' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1292:in `block in execute_migration_in_transaction' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1343:in `block in ddl_transaction' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `block in transaction' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:239:in `block in within_new_transaction' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:236:in `within_new_transaction' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `transaction' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:212:in `transaction' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1343:in `ddl_transaction' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1291:in `execute_migration_in_transaction' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1263:in `block in migrate_without_lock' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1262:in `each' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1262:in `migrate_without_lock' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1210:in `block in migrate' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1363:in `with_advisory_lock' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1210:in `migrate' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1036:in `up' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/migration.rb:1011:in `migrate' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/tasks/database_tasks.rb:172:in `migrate' /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <top (required)>' /app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.3/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' /app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.3/lib/rails/commands/rake/rake_command.rb:20:in `perform' /app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.3/lib/rails/command.rb:48:in `invoke' /app/vendor/bundle/ruby/2.5.0/gems/railties-5.2.3/lib/rails/commands.rb:18:in `<top (required)>' /app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require' /app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `block in require_with_bootsnap_lfi' /app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register' /app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require_with_bootsnap_lfi' /app/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `block in require' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in `load_dependency' /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in `require' /app/bin/rails:9:in `<main>' Caused by: ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column "opened" cannot be cast automatically to type integer HINT: You might need to specify "USING opened::integer". : ALTER TABLE "dreams" ALTER COLUMN "opened" TYPE integer (中略) Caused by: PG::DatatypeMismatch: ERROR: column "opened" cannot be cast automatically to type integer HINT: You might need to specify "USING opened::integer". (中略)

該当のソースコード

column "opened" cannot be cast automatically to type integer HINT: You might need to specify "USING opened::integer".

この文言が複数回出てきています。openedカラムがintegerにうまく変更できなかったからエラーが出ていると理解しました。

試したこと

HINTにもあるように、また開発環境と本番環境を分けてchange_columnしてみましたが結果は変わらず。

class ChangeColumn < ActiveRecord::Migration[5.2] def up if Rails.env.development? || Rails.env.test? change_column :dreams, :opened, :integer else Rails.env.production? change_column :dreams, :opened, 'integer USING opened::integer' end end end

removeとaddで作り直してみても結果は変わらず。

class AddAndRemoveColumn < ActiveRecord::Migration[5.2] def up remove_column :dreams, :opened add_column :dreams, :opened, :integer end def down remove_column :dreams, :opened add_column :dreams, :opened, :integer end end

解決へのヒントをいただければ幸いです。

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

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

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

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

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

winterboum

2019/07/06 09:01

結果は変わっていると思います。失敗したというのはおなじでもその後の PG::DatatypeMismatch: ERROR: column "opened" cannot be cast automatically to type integer HINT: You might need to specify "USING opened::integer". が、少なくとも最後の removeとaddで作り直した時は違うと。 最後のでうまく行かないのはちょっと解せないので、そこのメッセージを見せて下さい。 それと、 rails db:migate:status を
f--o

2019/07/06 09:18 編集

removeとaddをした後のエラーも同じでした。 PG::DatatypeMismatch: ERROR: column "opened" cannot be cast automatically to type integer HINT: You might need to specify "USING opened::integer". : ALTER TABLE "dreams" ALTER COLUMN "opened" TYPE integer (以下略) そもそも、migrationファイル作成→rails db:migrate→githubに適用→git push heroku master→heroku run rails db:migrateという順番で行っているのですがこちらに間違いはないでしょうか?
winterboum

2019/07/06 10:02

> removeとaddをした後のエラーも同じでした。 とありますが、removeとaddをした時のエラーがPG::DatatypeMismatch: ERROR:ですか?
f--o

2019/07/06 10:05

はい、そうです。する前と後のエラー文が全く同じだと思います。
winterboum

2019/07/06 10:41

する前、する後 というのが?? した時のを伺ってます。
f--o

2019/07/06 11:17

migrationファイルを作成し、openedカラムをremoveかつintegerでaddした後に、rails db:migrate→githubに適用→git push heroku master→heroku run rails db:migrateという作業を行ったところ、先に書きましたエラーが出ております(最初に出たエラーと全く同じだと思います)。分かりにくくて申し訳ありません。
winterboum

2019/07/06 19:35

removeやaddに対してcolumn "opened" cannot be cast となるのが解せません。 ということは その前のmigrationが未だ適用されずに残っているのだと思われます。 herokuでの 1) rails db:migrate:status を行った結果 2) その中で down となったmigrationのcode を見せて下さい
f--o

2019/07/07 00:10 編集

1)今までherokuでrails db:migrate:statusを行っていませんでした。かなり前の段階でdownになっていました… up 20190702062357 Devise create users up 20190702062508 Create dreams down 20190702082636 Change data opened to dreams down 20190702083223 Change data opened of dreams down 20190702084701 Change data opened of dream down 20190702093305 Change data of dream down 20190703081811 Add row order to dreams down 20190706062456 Change column down 20190706063509 Change column integer down 20190706065024 Remove and add column down 20190706070049 Add and remove column down 20190706084007 Change type down 20190706091312 Addandremovecolumn down 20190706095335 Change opened 2)最初にdownになっているmigrationは以下です。最初に型を変更した時のものだと思います。 class ChangeDataOpenedToDreams < ActiveRecord::Migration[5.2] def change change_column :dreams, :opened, :integer end end
guest

回答1

0

ベストアンサー

localとherokuでDBにズレがでていますから、それを直し、次に修正に進みましょう。

localもここまで(20190702062508)戻して大丈夫ですか?
up 20190702062357 Devise create users
up 20190702062508 Create dreams

でしたら、localで
rails db:migrate VERSION=20190702062508

次に、不要なmigrationを削除します。
例えば20190702082636 Change data opened to dreams が不要なら

git rm db/migrates/20190702082636_change_data_opened_to_dreams.rb

を行ってから commit
で、それをherokuにpushします。

もしまだデータが入っていないなら、20190702062357 Devise create users までもどして、CreateDreamesからやり直したほうがよいかも。

migrationは失敗したものがあったら、それを解決しておかないと先に進みません。うまく行かなかった場合は Gitからも削除するか、修正してcommitするかして解決して下さい。

データが溜まってからですと、booleanからintegerにする必要がでてくることもあるかと。それはまた相談して下さい

投稿2019/07/07 01:33

winterboum

総合スコア23329

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

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

f--o

2019/07/07 06:45 編集

ご回答ありがとうございます。 CreateDreamsからやり直すことにして、上記の手順を行っていき、usersテーブルのみがある状態では無事herokuでmigrateできました。 ただ、その後dreamsテーブルをローカルで作成しherokuでmigrateしようとしたところ、 PG::DuplicateTable: ERROR: relation "dreams" already exists というエラーが出ました。 pgの方ではdreamsテーブルがうまく削除できていないということだと思うのですが、解決策にたどり着けておりません。 herokuのmigrate:statusは以下の状態です。 up 20190702062357 Devise create users up 20190702062508 ********** NO FILE ********** down 20190707035438 Create dreams
winterboum

2019/07/07 07:48

20190702062508 Create dreams 消すまえに db:migrate VERSION=20190702062357 しました? VERSION=20190702062508 でやっちゃいましたね。 2)良い子がしてはいけない方法で逃げるか、 1)Userしかないなら完全に作りなおすか。 2)は。 2-1 20190707035438 Create dreams を20190702062357Create dreams の名前に書き換えて、 2-2 db:migrete VERSION=20190702062357  して 2-3 20190707035438 Create dreamsに戻してmigrate 1は db:drop して db:create して db:migrate なのですが、herokuで行う場合 環境変数 RAILS_ENV をどう設定するのが正しいのかわからないんで、コソコソ、、、
f--o

2019/07/07 10:10

おっしゃる通りのミスをしておりました。 2の方法を試み、無事migrateすることができました(ユーザー詳細ページにアクセスできない等の問題はまだあるのですが)。 今回の件で色々と勉強になりました。本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問