実現したいこと
開発環境で発生しているエラーを解決して本番環境(heroku)に反映させたい
エラーが出ておこなったこと
herokuにデプロイした後に投稿アプリの動作を試したところ、投稿の削除機能のところでエラーが出ました。
'heroku logs'で調べたところ、以前に間違えて作ってしまったマイグレーションの'image_id'が原因だと思ったため、'rails g migration RemoveNameFromTweets image_id:string'を行いました。そして'rails db:migrate'、'heroku run rake db:migrate'を行いました。
###発生したエラー・ターミナル
投稿を削除しようとした時
2020-08-29T16:07:23.618829+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] Tweet Destroy (4.9ms) DELETE FROM `tweets` WHERE `tweets`.`id` = 11 2020-08-29T16:07:23.623514+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] (4.5ms) ROLLBACK 2020-08-29T16:07:23.629723+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] Completed 500 Internal Server Error in 33ms (ActiveRecord: 20.7ms | Allocations: 2836) 2020-08-29T16:07:23.630340+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] 2020-08-29T16:07:23.630340+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] NoMethodError (undefined method `image_id' for #<Tweet:0x00005654eecd6038> 2020-08-29T16:07:23.630341+00:00 app[web.1]: Did you mean? image): 2020-08-29T16:07:23.630341+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] 2020-08-29T16:07:23.630342+00:00 app[web.1]: [d54d13b6-d44c-45d0-977e-874c9fc672ca] app/controllers/tweets_controller.rb:44:in `destroy' 2020-08-29T16:07:23.631916+00:00 heroku[router]: at=info method=POST path="/tweets/11" host=backbooone.herokuapp.com request_id=d54d13b6-d44c-45d0-977e-874c9fc672ca fwd="219.115.150.165" dyno=web.1 connect=0ms service=37ms status=500 bytes=1827 protocol=https
heroku run rake db:migrateした後
StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Can't DROP 'image_id'; check that column/key exists
rails db:migrate:resetも行いました
Dropped database 'Backbone_development' Dropped database 'Backbone_test' Created database 'Backbone_development' Created database 'Backbone_test' == 20200820142448 DeviseCreateUsers: migrating ================================ -- create_table(:users) -> 0.0204s -- add_index(:users, :username, {:unique=>true}) -> 0.0173s -- add_index(:users, :email, {:unique=>true}) -> 0.0113s -- add_index(:users, :reset_password_token, {:unique=>true}) -> 0.0090s == 20200820142448 DeviseCreateUsers: migrated (0.0583s) ======================= == 20200823132550 CreateTweets: migrating ===================================== -- create_table(:tweets) -> 0.0154s == 20200823132550 CreateTweets: migrated (0.0154s) ============================ == 20200827063550 CreateFavorites: migrating ================================== -- create_table(:favorites) -> 0.0390s == 20200827063550 CreateFavorites: migrated (0.0391s) ========================= == 20200829155203 RemoveNameFromTweets: migrating ============================= -- remove_column(:tweets, :image_id, :string) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Can't DROP 'image_id'; check that column/key exists /Users/maedashuya/projects/Backbone/db/migrate/20200829155203_remove_name_from_tweets.rb:3:in `change' /Users/maedashuya/projects/Backbone/bin/rails:9:in `<top (required)>' /Users/maedashuya/projects/Backbone/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Can't DROP 'image_id'; check that column/key exists /Users/maedashuya/projects/Backbone/db/migrate/20200829155203_remove_name_from_tweets.rb:3:in `change' /Users/maedashuya/projects/Backbone/bin/rails:9:in `<top (required)>' /Users/maedashuya/projects/Backbone/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: Mysql2::Error: Can't DROP 'image_id'; check that column/key exists /Users/maedashuya/projects/Backbone/db/migrate/20200829155203_remove_name_from_tweets.rb:3:in `change' /Users/maedashuya/projects/Backbone/bin/rails:9:in `<top (required)>' /Users/maedashuya/projects/Backbone/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:migrate:reset => db:migrate (See full trace by running task with --trace)
rails db:migrateした後
== 20200829155203 RemoveNameFromTweets: migrating ============================= -- remove_column(:tweets, :image_id, :string) rails aborted! StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Can't DROP 'image_id'; check that column/key exists /Users/maedashuya/projects/Backbone/db/migrate/20200829155203_remove_name_from_tweets.rb:3:in `change' /Users/maedashuya/projects/Backbone/bin/rails:9:in `<top (required)>' /Users/maedashuya/projects/Backbone/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Can't DROP 'image_id'; check that column/key exists /Users/maedashuya/projects/Backbone/db/migrate/20200829155203_remove_name_from_tweets.rb:3:in `change' /Users/maedashuya/projects/Backbone/bin/rails:9:in `<top (required)>' /Users/maedashuya/projects/Backbone/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: Mysql2::Error: Can't DROP 'image_id'; check that column/key exists /Users/maedashuya/projects/Backbone/db/migrate/20200829155203_remove_name_from_tweets.rb:3:in `change' /Users/maedashuya/projects/Backbone/bin/rails:9:in `<top (required)>' /Users/maedashuya/projects/Backbone/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Tasks: TOP => db:migrate (See full trace by running task with --trace)
rails db:migrate:status
database: Backbone_development Status Migration ID Migration Name -------------------------------------------------- up 20200820142448 Devise create users up 20200823132550 Create tweets up 20200827063550 Create favorites down 20200829155203 Remove name from tweets
追記
create_table :users do |t| ## Database authenticatable t.string :username, null: false t.text :profile t.string :profile_image_id t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: ""
class CreateTweets < ActiveRecord::Migration[6.0] def change create_table :tweets do |t| t.integer :user_id t.string :title t.text :body t.timestamps end end end
class CreateFavorites < ActiveRecord::Migration[6.0] def change create_table :favorites do |t| t.integer :user_id t.integer :tweet_id t.timestamps end end end
class RemoveNameFromTweets < ActiveRecord::Migration[6.0] def change remove_column :tweets, :image_id, :string end end
ActiveRecord::Schema.define(version: 2020_08_23_132550) do create_table "tweets", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.integer "user_id" t.string "title" t.text "body" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "username", null: false t.text "profile" t.string "profile_image_id" t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["username"], name: "index_users_on_username", unique: true end end
class Tweet < ApplicationRecord belongs_to :user attachment :image has_many :favorites, dependent: :destroy with_options presence: true do validates :title validates :body end end
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :username, presence: true, uniqueness: true attachment :profile_image has_many :tweets, dependent: :destroy has_many :favorites, dependent: :destroy def already_favorited?(tweet) self.favorites.exists?(tweet_id: tweet.id) end end
class Favorite < ApplicationRecord belongs_to :user belongs_to :tweet validates_uniqueness_of :tweet_id, scope: :user_id end