[rails] error during devise installation

I got the following error while installing devise. After bundle install gem, run rails g devise: install. After that, after executing rails g devise user, it is an error that occurred when executing rake db: migrate.

error

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)
=================================

Solutions I was a beginner and didn't know what was going on, so I checked which one of the current tables is working.

rails db:migrate status

result

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

Before bundle install devise here I noticed that the user table that I was making as a trial has been created.

You may not need it, but once you do the following to get the table back

rails db:rollback

All are down. Then delete unnecessary 20201014013111 Create users

rm -rf db/migrate/20201014013111_create_users.rb

After dropping the table

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

And

〇〇-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:

I got an additional error. The cause is that when devise is installed, what can be created by create is changed and an error occurs. I modified it below.

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

Then removed to uninstall 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

And finally, I installed devise again and it works fine.

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

Since I am a beginner, it took me a few hours. I hope it helps someone.

reference Qiita: [Rails] Delete migration file https://qiita.com/ISSO33/items/33a935cb3255c269bef2

Qitta: Rails production environment ActiveRecord :: StatementInvalid (Mysql2 :: Error: Table'table name' doesn't exist): https://qiita.com/ashketcham/items/a0d0a2ac788779895fb4

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

Uninstall Qiita: devise https://qiita.com/NT90957869/items/8c5285775a67a51f03e1

teratall: [rails] error during devise installation https://teratail.com/questions/214948

Recommended Posts

[rails] error during devise installation
[Rails] gem devise installation flow
[Rails] devise
[Rails] Unexpected validation error in devise
[Rails] Introducing devise
rails + devise + devise_token_auth
The road to Japaneseizing Rails devise error messages
[Note] Rails error list
Catch Rails Routing Error
[Rails] devise helper method
[Rails] Customize devise validation
[Rails error] unexpected tIDENTIFIER
Handle devise with Rails
rails heroku error log
rails error resolution summary
[Rails] devise introduction method
Rails, RSpec installation procedure
[rails] About devise defaults
Japaneseization of Rails error messages [devise / Form objects, etc.]
[Rails] About error resolution when installing devise and activeadmin
[Rails] Introduction of devise Basics
What is Rails gem devise?
rails error Library not loaded
[Rails] Workaround for classes automatically generated by devise error messages
[Rails] How to install devise
ActiveRecord :: NotNullViolation in Devise error
Error in rails db: migrate
(For beginners) [Rails] Install Devise
[Rails] Rails version upgrade error memorandum
Memorandum [Rails] User authentication Devise
[Rails] What was the error message?
[Rails] How to use gem "devise"
[Rails] Japanese localization of error messages
[Rails] How to use devise (Note)
[Rails] Session timeout setting in devise
[Devise] rails memo basic setting initial setting
[Rails] Display form error messages asynchronously
[Rails] [Devise] Edit profile without password
Japanese localization of error messages (rails)
Ruby On Rails devise routing conflict
[Rails] syntax error, unexpected tSTRING_END, expecting'''
[Rails] AWS deployment error encounter summary
What to do if you get an error during rails db: reset
Rails Tutorial record and memorandum # 1 "From installation to hello_app deployment + error handling"