1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[rails] devise導入中のエラー

Posted at
1 / 2

devise導入中に下記のエラーが出ました。
gemをbundle installしたのち、rails g devise:installを実行。
その後、rails g devise userを実行したのち、rake db:migrateを実行した時に発現したエラーです。

エラー

Mysql2::Error: Table 'development.users' doesn't exist
/Users/projects/db/migrate/20201016082907_add_devise_to_users.rb:7:in `block in up'
/Users/projects/db/migrate/20201016082907_add_devise_to_users.rb:5:in `up'
/Users/projects/bin/rails:9:in `<top (required)>'
/Users/projects/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'development.users' doesn't exist
/Users/projects/db/migrate/20201016082907_add_devise_to_users.rb:7:in `block in up'
/Users/projects/db/migrate/20201016082907_add_devise_to_users.rb:5:in `up'
/Users/projects/bin/rails:9:in `<top (required)>'
/Users/projects/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Caused by:
Mysql2::Error: Table 'development.users' doesn't exist
/Users/projects/db/migrate/20201016082907_add_devise_to_users.rb:7:in `block in up'
/Users/projects/db/migrate/20201016082907_add_devise_to_users.rb:5:in `up'
/Users/projects/bin/rails:9:in `<top (required)>'
/Users/projects/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

結果

〇〇-no-MacBook% rake db:migrate:status
database: development
 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20201013135213  Create items
   up    20201014013111  Create users
  down    20201016082907  Add devise to users

ここでdeviseをbundle installする前に、
試しに作っていた、userテーブルができてしまっていることに気がつきました。

必要ないかもしれないですが一度テーブルを元に戻すために下記を実行

rails db:rollback

全てdownの状態にしました。
その後、不必要な、20201014013111 Create usersを削除

rm -rf db/migrate/20201014013111_create_users.rb

テーブルを削除した後、

〇〇-no-MacBook% rake db:migrate:status                          
database: NF_development
 Status   Migration ID    Migration Name
--------------------------------------------------
  down    20201013135213  Create items
  down    20201016082907  Add devise to users

そして

〇〇-no-MacBook% rails db:migrate                                   
== 20201013135213 CreateItems: migrating ======================================
-- create_table(:items)
   -> 0.0173s
== 20201013135213 CreateItems: migrated (0.0174s) =============================

== 20201016082907 AddDeviseToUsers: migrating =================================
-- change_table(:users)
rails aborted!
StandardError: An error has occurred, all later migrations canceled:

さらにエラーが出てしまいました。
原因は、deviseをinstallした際に本来、createでできるものがchangeになっておりエラーが発生
下記に修正しました。

class CreateItems < ActiveRecord::Migration[6.0]
  def change→create
    change→create_table :items do |t|
      t.string :name
      t.string :text
      t.text :image
      t.integer :category_id
      t.integer :item_id
      t.timestamps
    end
  end
end

その後、deviseをアンインストールするために削除

〇〇-no-MacBook% rails d devise user             
Running via Spring preloader in process 73770
Deprecation warning: Expected boolean default value for '--orm'; got :active_record (string).
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
      invoke  active_record
      remove    db/migrate/20201016082907_add_devise_to_users.rb
      remove    app/models/user.rb
      invoke    test_unit
      remove      test/models/user_test.rb
      remove      test/fixtures/users.yml
       route  devise_for :users

そして最後に、再度deviseをinstallし正常に動くようになりました。

〇〇-no-MacBook%rails g devise user 
Running via Spring preloader in process 73899
Deprecation warning: Expected boolean default value for '--orm'; got :active_record (string).
This will be rejected in the future unless you explicitly pass the options `check_default_type: false` or call `allow_incompatible_default_type!` in your code
You can silence deprecations warning by setting the environment variable THOR_SILENCE_DEPRECATION.
      invoke  active_record
      create    db/migrate/20201016152820_devise_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      insert    app/models/user.rb
       route  devise_for :users

初学者なので2,3時間かかってしまいました。
誰かのお役に立てれば嬉しいです。

参考
Qitta:【Rails】マイグレーションファイルの削除
https://qiita.com/ISSO33/items/33a935cb3255c269bef2

Qitta:Rails 本番環境 ActiveRecord::StatementInvalid (Mysql2::Error: Table 'テーブル名' doesn't exist):
https://qiita.com/ashketcham/items/a0d0a2ac788779895fb4

Qiita:Mysql2::Error::ConnectionError: Access denied for user
'root'@'localhost' (using password: YES)の解決法
https://qiita.com/naota7118/items/b62d71484e21d6739d68

Qiita:deviseのアンインストール
https://qiita.com/NT90957869/items/8c5285775a67a51f03e1

teratall:[rails] devise導入中のエラー
https://teratail.com/questions/214948

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?