簡體   English   中英

Rails:帶有PG gem的Sqlite

[英]Rails: Sqlite with PG gem

我在Mac上安裝了postgres並且第一次使用Rails進行了嘗試。 我包括了寶石“pg”並刪除了sqlite3 gem(畢竟,如果使用前者,為什么你需要后者)。 但是,當我嘗試啟動服務器時,我收到此錯誤消息

.rvm/gems/ruby-1.9.3-rc1@rails321/gems/bundler-1.0.22/lib/bundler/rubygems_integration.rb:143:in `block in replace_gem': Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` (sqlite3 is not part of the bundle. Add it to Gemfile.) (LoadError)

所以我再次包含sqlite3 gem,現在服務器工作正常,但我實際上不知道我的測試應用程序是否使用sqlite3或pg?

a)如果我打算使用pg gem,我是否應該安裝sqlite3 gem? b)如果我只應該安裝兩個中的一個,有沒有辦法找出我的測試應用當前正在使用哪一個(因為它們都在Gemfile中)

的Gemfile

source 'https://rubygems.org'

gem 'rails', '3.2.1'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'pg'
gem 'devise'
gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer'

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

這是我的database.yml,當我使用pg gem時,適配器實際上稱為postgresql,並且在設置中還有一些其他差異,如果你只是復制並粘貼下面的代碼並更改數據庫名稱你應該很漂亮准備好了(我使用了heroku,這在那里工作):

development:
  adapter: postgresql
  encoding: utf8
  reconnect: false
  database: DATABASE_DEVELOPMENT
  pool: 5
  username: USER_NAME
  password:
  host: localhost

test:
  adapter: postgresql
  encoding: utf8
  reconnect: false
  database: DATABASE_TEST
  pool: 5
  username: USER_NAME
  password:
  host: localhost

production:
  adapter: postgresql
  encoding: utf8
  reconnect: false
  database: DATABASE_PRODUCTION
  pool: 5
  username: root
  password:

目前,您在同一環境中安裝兩個數據庫 - 根據Gemfile

您可能會在一個Gemfile中的不同環境中使用sqlite和pg。

如果你想使用

gem 'sqlite3'

group :production do
  gem 'pg', '0.12.2'
end

所以現在我在開發模式和生產中使用sqlite3我正在使用pg,所以在你的database.yml中你需要放置兩個連接,首先是開發模式和生產模式

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: pg (please correct the adapter)
  database: 
  user:
  password:

如果您需要更多幫助,請告訴我

A)如果我打算使用pg gem,我是否應該安裝sqlite3 gem?

不,因為你懷疑你需要sqlite的sqlite gem和postgres的pg gem

B)如果我只應該安裝兩個中的一個,有沒有辦法找出我的測試應用當前正在使用哪一個(因為它們都在Gemfile中)

是。 鍵入: rails db並查看輸出。 以下是postgres的內容:

$rails db
psql (9.1.2)
Type "help" for help.

C)反問道:“為什么我需要兩者?”

實際上,有幾種情況你需要兩者 ,其中包括一個數據庫中的一些現有數據,另一個,將應用程序從一個轉換為另一個,等等。然而,它通常是一個或另一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM