Home>

After changing from "sqllite" to "mysql" in rails, the command of "rails g model table name column" cannot be executed.

The error code is as follows

matsurawataru @ matsuurawatarunoMacBook-Pro pokeGO% rails g model Pokemon
/Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/railties-6.0.3.4/lib/rails/application/configuration.rb:231:in `database_configuration': Cannot load database configuration:
undefined method `each'for" use # SQLite. Versions 3.8.0 and up are supported. ": String (NoMethodError)
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/railtie.rb:200: in `block (2 levels) in<class: Railtie>'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:71:in `class_eval'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:71: in `block in execute_hook'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:51:in `each'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activesupport-6.0.3.4/lib/active_support/lazy_load_hooks.rb:51:in `run_load_hooks'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/base.rb:327:in `<module: ActiveRecord>'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.3.4/lib/active_record/base.rb:27:in `<main>'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require '
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi '
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/bootsnap-1.4.9/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require '
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/zeitwerk-2.4.0/lib/zeitwerk/kernel.rb:34:in `require'from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:376:in `active_record_configured?'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:293:in `disconnect_database'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:111: in `preload'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:157: in `serve'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top ( required)>'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/matsurawataru/.rbenv/versions/2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e: 1: in `<main>'

The code for database.yml is as follows

use # SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
#Ensure the SQLite 3 gem is defined in your Gemfile
# gem'sqlite3'
#
default:&default
  adapter: mysql2
  encoding: utf8pool: 5
  username: root
  password: root
  host: localhost

development:
<<: * default
  database: pokeGO_development # DB name. It is easy to understand if you change the "sample" part to the name of the application
  username: pokemonGO
  password: pokepoke
test: test:
<<: * default
  database: pokeGO_test # DB name. It is easy to understand if you change the "sample" part to the name of the application
production:
<<: * default
  database: pokeGO_production
  username: pokeGO_app
  password:<% = ENV ['SAMPLE_DATABASE_PASSWORD']%>

I don't know which other file to put the code in, so I will add it if necessary.

If anyone can understand it, thanks for your professor.

  • Answer # 1

    Comment out the first line of the database file.

  • Answer # 2

    Comment out the first line of the database. file.