我是Rails的新手,在完成Heroku教程后,我使用PostgresQL设置了一个项目。
一位朋友也学习了Rails,他们使用了一个不同的教程,该教程使用SQLite作为数据库,我已经克隆了。
使用SQLite项目启动rails服务器时,我不断收到错误,提示该项目正在尝试使用PostgresQL并告诉我将gem pg
添加到我的Gemfile中。
我在网上找到的所有内容都是关于从MySQL / SQLite /等迁移到PostgresQL的各种帖子,但我不打算迁移。
我的database.yml如下:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# 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:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
RubyMine中报告的错误是:
/usr/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/Ty/Rails/TSCLunch/web/bin/rails server -b 0.0.0.0 -p 3000 -e development
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/Users/Ty/.gem/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:177:in `rescue in spec': Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord). (Gem::LoadError)
from /Users/Ty/.gem/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/connection_specification.rb:174:in `spec'
from /Users/Ty/.gem/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/connection_handling.rb:50:in `establish_connection'
from /Users/Ty/.gem/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
from /Users/Ty/.gem/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
from /Users/Ty/.gem/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
from /Users/Ty/.gem/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
from /Users/Ty/.gem/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:27:in `each'
from /Users/Ty/.gem/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
from /Users/Ty/.gem/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/railtie.rb:116:in `block in <class:Railtie>'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `instance_exec'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in `run'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/tsort.rb:180:in `each'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/initializable.rb:54:in `run_initializers'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/application.rb:352:in `initialize!'
from /Users/Ty/Rails/TSCLunch/web/config/environment.rb:5:in `<top (required)>'
from /Users/Ty/.gem/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /Users/Ty/.gem/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/Ty/.gem/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/Ty/.gem/ruby/2.0.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /Users/Ty/Rails/TSCLunch/web/config.ru:3:in `block in <main>'
from /Users/Ty/.gem/ruby/2.0.0/gems/rack-1.6.0/lib/rack/builder.rb:55:in `instance_eval'
from /Users/Ty/.gem/ruby/2.0.0/gems/rack-1.6.0/lib/rack/builder.rb:55:in `initialize'
from /Users/Ty/Rails/TSCLunch/web/config.ru:in `new'
from /Users/Ty/Rails/TSCLunch/web/config.ru:in `<main>'
from /Users/Ty/.gem/ruby/2.0.0/gems/rack-1.6.0/lib/rack/builder.rb:49:in `eval'
from /Users/Ty/.gem/ruby/2.0.0/gems/rack-1.6.0/lib/rack/builder.rb:49:in `new_from_string'
from /Users/Ty/.gem/ruby/2.0.0/gems/rack-1.6.0/lib/rack/builder.rb:40:in `parse_file'
from /Users/Ty/.gem/ruby/2.0.0/gems/rack-1.6.0/lib/rack/server.rb:299:in `build_app_and_options_from_config'
from /Users/Ty/.gem/ruby/2.0.0/gems/rack-1.6.0/lib/rack/server.rb:208:in `app'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/server.rb:61:in `app'
from /Users/Ty/.gem/ruby/2.0.0/gems/rack-1.6.0/lib/rack/server.rb:336:in `wrapped_app'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/server.rb:139:in `log_to_stdout'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/server.rb:78:in `start'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:80:in `block in server'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `tap'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `server'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/Ty/.gem/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/Ty/Rails/TSCLunch/web/bin/rails:4:in `require'
from /Users/Ty/Rails/TSCLunch/web/bin/rails:4:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
Process finished with exit code 1
答案 0 :(得分:2)
您可以通过两种方式为ActiveRecord指定适配器。
ENV['DATABASE_URL']
环境变量database.yml
配置文件如果同时设置了config / database.yml和ENV ['DATABASE_URL'],则Rails会将配置合并在一起。我认为这是导致问题的原因。
如果是这样,您可以删除环境变量或在database.yml
我建议您阅读以下文章以获取更多信息:http://guides.rubyonrails.org/configuring.html#configuring-a-database
答案 1 :(得分:0)
我假设您只想获得一个“有效”的项目&#39;本地而不是heroku,不管它是否使用Postgres或SQLITE3,是吗?
你也可以发布Gemfile的副本吗?由于您遵循了Heroku教程,从您的帖子中不清楚您遵循的其他步骤,除了您可能通过在命令行上运行此应用程序来创建您的应用程序:
$&GT; rails new myapp --database = postgresql
这会添加&#34; gem&#39;&#39;&#34;到你的Gemfile,它会为它创建一个合适的database.yml。但是如果你复制了指定SQLITE3的其他database.yml,那么现在你的Rails环境就会混淆。
如果你有宝石pg&#39;在您的Gemfile中,请尝试以下步骤:
1)在您的Gemfile中,删除&#39; pg&#39;换行并替换为: gem&#39; sqlite3&#39;
2)进行捆绑安装&#39;安装克隆的database.yml指定的SQLITE3。
3)运行以下命令重置数据库: $&GT; rake db:reset
希望这会让你再次前进。现在你应该有一个使用SQLITE3而不是Postgres的项目。您无法将其部署到Heroku,但因为他们不允许SQLITE3进行生产。
如果你使用SQLITE3在本地工作,以后想要部署到Heroku,它们提供了一个简单的过程,从SQLITE3移回Postgres:https://devcenter.heroku.com/articles/sqlite3