Home Articles

heroku rake db:migrate在rails 4中不起作用

Asked
Viewed 1442 times
0

当我做 rake db:migrate 时,我得到错误,下面我已经附加了我的databse.yml和我的宝石文件,尝试了互联网上的所有东西,但错误没有得到解决!

rake aborted!
LoadError: cannot load such file -- mysql2

development:
  adapter: mysql2
  encoding: utf8
  database: demo_project_development
  pool: 5
  username: root
  password: root
  socket: /var/run/mysqld/mysqld.sock
  host: localhost
test:
  adapter: mysql2
  database: demo_project_test
  database: db/development.mysql2
  username: root
  password: root
  pool: 5
  timeout: 5000
production:
  adapter: postgresql
  database: demo_project_production
  pool: 5
  timeout: 5000

Gemfile源'https://rubygems.org ' gem ' rails ', ' 4.2.6 ' gem ' sass-rails ', '~> 5.0 ' gem ' uglifier ', '> = 1.3.0 ' gem ' coffee-rails ', '~> 4.1.0 ' gem ' jquery-rails ' gem ' turbolinks ' gem ' jbuilder ', '~> 2.0 ' gem ' sdoc ', '~> 0.4.0',group :: doc

group :production do
  gem 'pg', '0.20'
  gem 'rails_12factor'
end

group :test do
  gem 'byebug'
  gem 'mysql2', '>= 0.3.13', '< 0.5'
end

gem 'spring'
gem 'devise'
gem "cancan"
gem 'ckeditor', '4.1.3'
gem "nested_form"
gem "paperclip", "~> 5.0.0"
gem 'bootstrap-sass', '~> 3.3.6'
gem 'kaminari'
gem 'ratyrate'
gem 'thinking-sphinx', '~> 3.3.0'
gem 'delayed_job_active_record'
gem 'rails-api'
gem 'active_model_serializers', '~> 0.10.6'
group :development do
  gem 'web-console', '~> 2.0'
  gem 'mysql2', '>= 0.3.13', '< 0.5'
end

下面是我的application.rb

require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module DemoProject
  class Application < Rails::Application
    config.active_record.raise_in_transactional_callbacks = true
    config.active_job.queue_adapter = :delayed_job
    config.api_only = false
  end
end

配置/ envoirment.rb

require File.expand_path('../application', __FILE__)
Rails.application.initialize!

的boot.rb

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' # Set up gems listed in the Gemfile.

1 Answer

  • 0

    我能够使用 Gemfiledatabase.yml 在虚拟Rails 4.2.6应用程序中重现您的错误 . 我甚至无法在没有提升 LoadError: cannot load such file -- mysql2 的情况下将应用程序推送到Heroku .

    我通过从开发和测试组中删除 mysql2 gem并将其添加到默认组来修复此问题 . 我的猜测是在本地运行 bundle install 将它作为依赖项添加到 Gemfile.lock ,它被检入git并推送到Heroku . Heroku不会安装gem,当应用程序试图要求它时,会引发 LoadError .

    group :test do
      gem 'byebug'
    end
    
    group :development do
      gem 'web-console', '~> 2.0'
    end
    
    gem 'mysql2', '>= 0.3.13', '< 0.5'
    

    几个注意事项:如果要将一个gem添加到两个或更多特定组,请不要单独指定它们 . 这只意味着有两个地方你必须记住改变你的版本依赖 . 所以不要这样

    group :test do
      gem 'byebug'
      gem 'another_gem'
    end
    
    group :development do
      gem 'web-console', '~> 2.0'
      gem 'another_gem'
    end
    

    做这个

    group :test do
      gem 'byebug'
    end
    
    group :development do
      gem 'web-console', '~> 2.0'
    end
    
    group :development, :test do
      gem 'another_gem'
    end
    

    此外,你不应该在本地使用MySQL和PostgreSQL,只需使用Postgres . 它们并不完全兼容,因此您必将遇到令人困惑的问题 . 解决此问题的最佳方法是完全删除 mysql2 并为Postgres配置开发和测试数据库 .

Related