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

时间:2017-08-04 14:30:57

标签: ruby-on-rails ruby-on-rails-4

当我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'     宝石' rails',' 4.2.6'     宝石' sass-rails','〜> 5.0'     gem' uglifier','> = 1.3.0'     gem' coffee-rails','〜> 4.1.0'     gem' jquery-rails'     宝石' turbolinks'     宝石' jbuilder','〜> 2.0'     宝石' 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 个答案:

答案 0 :(得分:0)

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

我通过从开发和测试组中删除mysql2 gem并将其添加到默认组来修复此问题。我的猜测是,在本地运行bundle install会将其作为依赖项添加到Gemfile.lock,并将其检入git并推送到Heroku。 Heroku没有安装宝石,当应用程序试图要求它时,会引发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配置开发和测试数据库。