我按照其他解决方案的建议添加了gem mysql2,然后尝试创建模型,但是我遇到了同样的错误。
下面是我的database.yml文件和错误消息。另外,请注意我正在学习一个教程,我需要首先生成模型,然后运行db:migrate。
default: &default
adapter: mysql
database: myrubyblog
username: root
password: root123
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
adapter: mysql
database: myrubyblog
username: root
password: root123
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql
database: myrubyblog
username: root
password: root123
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
production:
adapter: mysql
database: myrubyblog
username: root
password: root123
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
sania@sania-Swift-SF314-52:~/myrubyblog$ rails g model category name:string
/home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require': Could not load 'active_record/connection_adapters/mysql_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile. (LoadError)
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/connection_specification.rb:186:in `spec'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:880:in `establish_connection'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/connection_handling.rb:58:in `establish_connection'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/railtie.rb:124:in `block (2 levels) in <class:Railtie>'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:69:in `instance_eval'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:69:in `block in execute_hook'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:60:in `with_execution_control'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:65:in `execute_hook'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:49:in `each'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/base.rb:326:in `<module:ActiveRecord>'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activerecord-5.1.6/lib/active_record/base.rb:25:in `<top (required)>'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:356:in `active_record_configured?'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:273:in `disconnect_database'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:107:in `preload'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:153:in `serve'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
from /home/sania/.rbenv/versions/2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
from -e:1:in `<main>'
答案 0 :(得分:1)
您必须在config/database.yml
文件中添加一行,以指定适配器为mysql2
而非mysql
。
adapter: mysql2
代替adapter: mysql
如果mysql2
的安装和捆绑正常,则可以正常使用。
您可能想要重新安装mysql2
。您可以使用以下命令
gem "mysql2"
bundle install
答案 1 :(得分:0)
您的数据库适配器错误,您的文件也有缩进问题,请尝试此代码
default: &default
adapter: mysql2
username: root
password: root123
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
<<: *default
database: myrubyblog_dev
test:
<<: *default
database: myrubyblog_test
production:
<<: *default
database: myrubyblog_prod
答案 2 :(得分:0)
您可以按照以下步骤解决此问题:
gem'mysql2' - 将此gem添加到您的Gemfile
捆绑安装或捆绑更新
adapter:mysql2 -Update you database.yml adapter
希望这会对你有所帮助:)。