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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

2回答

9039閲覧

rake db:migrateすると「StandardError: An error has occurred, all later migrations canceled:」エラーが起きる

takagitoshinari

総合スコア24

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2019/07/20 09:51

編集2019/07/20 10:00

前提・実現したいこと

ActiveRecord::PendingMigrationErrorが起きたのでrake db:migrateを実行したい。

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

rake db:migrateを実行すると以下のエラーが起きてしまう。

StandardError: An error has occurred, all later migrations canceled: Mysql2::Error: Table 'withme_development.users' doesn't exist: ALTER TABLE `users` ADD `email` varchar(255) DEFAULT '' NOT NULL /Users/takagitoshinari/withme/db/migrate/20190720085314_add_devise_to_users.rb:7:in `block in up' /Users/takagitoshinari/withme/db/migrate/20190720085314_add_devise_to_users.rb:5:in `up' Caused by: ActiveRecord::StatementInvalid: Mysql2::Error: Table 'withme_development.users' doesn't exist: ALTER TABLE `users` ADD `email` varchar(255) DEFAULT '' NOT NULL /Users/takagitoshinari/withme/db/migrate/20190720085314_add_devise_to_users.rb:7:in `block in up' /Users/takagitoshinari/withme/db/migrate/20190720085314_add_devise_to_users.rb:5:in `up' Caused by: Mysql2::Error: Table 'withme_development.users' doesn't exist /Users/takagitoshinari/withme/db/migrate/20190720085314_add_devise_to_users.rb:7:in `block in up' /Users/takagitoshinari/withme/db/migrate/20190720085314_add_devise_to_users.rb:5:in `up' Tasks: TOP => db:migrate (See full trace by running task with --trace)

該当のソースコード(20190720085314_add_devise_to_users.rb)

ruby

1# frozen_string_literal: true 2 3class AddDeviseToUsers < ActiveRecord::Migration[5.2] 4 def self.up 5 change_table :users do |t| 6 ## Database authenticatable 7 t.string :email, null: false, default: "" 8 t.string :encrypted_password, null: false, default: "" 9 t.string :nickname, null: false, default: "" 10 11 ## Recoverable 12 t.string :reset_password_token 13 t.datetime :reset_password_sent_at 14 15 ## Rememberable 16 t.datetime :remember_created_at 17 18 ## Trackable 19 # t.integer :sign_in_count, default: 0, null: false 20 # t.datetime :current_sign_in_at 21 # t.datetime :last_sign_in_at 22 # t.string :current_sign_in_ip 23 # t.string :last_sign_in_ip 24 25 ## Confirmable 26 # t.string :confirmation_token 27 # t.datetime :confirmed_at 28 # t.datetime :confirmation_sent_at 29 # t.string :unconfirmed_email # Only if using reconfirmable 30 31 ## Lockable 32 # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 33 # t.string :unlock_token # Only if unlock strategy is :email or :both 34 # t.datetime :locked_at 35 36 37 # Uncomment below if timestamps were not included in your original model. 38 # t.timestamps null: false 39 end 40 41 add_index :users, :email, unique: true 42 add_index :users, :reset_password_token, unique: true 43 # add_index :users, :confirmation_token, unique: true 44 # add_index :users, :unlock_token, unique: true 45 end 46 47 def self.down 48 # By default, we don't want to make any assumption about how to roll back a migration when your 49 # model already existed. Please edit below which fields you would like to remove in this migration. 50 raise ActiveRecord::IrreversibleMigration 51 end 52end 53

該当のソースコード(user.rb)

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable end

試したこと

rake db:migrate:resetを実行してみたがタイトルと同様のエラーが発生した。

補足情報(FW/ツールのバージョンなど)

ruby 2.3.1p112
Rails 5.2.3

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

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

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

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

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

guest

回答2

0

ベストアンサー

Mysql2::Error: Table 'withme_development.users' doesn't exist:
つまり、 table users が無いというエラーです。
migrationは
change_table :users do

です。tableが無いのにtableを修正しようとしています。

create_table の間違いか
さもなければ create_table :users の migration が無くなってしまったか

中身は create_table っぽい

投稿2019/07/20 13:44

winterboum

総合スコア23340

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

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

takagitoshinari

2019/07/20 14:49

指摘点を修正したところ無事解決致しました。 users tableが無いのにchange_tableしようとしていたことに気づけなかったのが悔しいです笑 助かりました。ありがとうございました!
winterboum

2019/07/21 11:52

エラーメッセージを丁寧に読めば今回は判ったのでは。 長いので諦めているのではと思いますが、 どこかの回答でどなたかがおしゃってましたが システムのエラーはまず無いから、自分のcodeについてのエラーメッセージを見ていけばたどり着けることが多いです
takagitoshinari

2019/07/31 12:45

ご指摘ありがとございます。 エラーに焦ってしまい、丁寧に読むことを怠っていました。 エラーに引かないように文章をしっかり読んでいこうと思います!!
guest

0

ALTER TABLE users ADD email varchar(255) DEFAULT '' NOT NULLに関するmigrationファイルが残っていたりすると起きるエラーな気がします。

もしもあればその部分を削除すれば良いのですが、色々と注意が必要なので、以下のURLを参考にしながら取り組んで見てください。
https://teratail.com/questions/149572

投稿2019/07/20 10:37

bamboo-nova

総合スコア1408

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

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

takagitoshinari

2019/07/20 14:52

原因としては、users tableが無いのにchange_tableしようとしていたことです。 change_tableではなくcreate_tableにすることで解決しました! 添付していただいたURLから新しい知識を得ることができました! ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問