2012-03-29 68 views
7

我是Stack Overflow和Ruby on Rails的新手。我的問題是,當我運行命令rake db:create或rake db:migrate時,測試數據庫會受到影響,但是開發數據庫不會。rake db:migrate和rake db:既在測試數據庫上創建,也不在開發數據庫

軌(3.2.2)

我的database.yml:

# 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: &test 
    adapter: postgresql 
    encoding: unicode 
    database: ticketee_test 
    pool: 5 
    username: ticketee 
    password: my_password_here 

development: 
    adapter: postgresql 
    encoding: unicode 
    database: ticketee_development 
    pool: 5 
    username: ticketee 
    password: my_password_here 

production: 
    adapter: postgresql 
    encoding: unicode 
    database: ticketee_production 
    pool: 5 
    username: ticketee 
    password: my_password_here 

cucumber: 
    <<: *test 

感謝您的建議,但恐怕不是這個原因。我的RAILS_ENV =發展。當我啓動rails服務器時,服務器運行開發數據庫。如果我運行「rails server -e test」,那麼服務器使用測試數據庫。我仍然不確定爲什麼遷移正在我的測試數據庫上運行...

更新: 基於一個建議,我註釋掉了除開發以外的所有數據庫配置,現在出現錯誤。我正在運行postgresql,我已經安裝了gem'pg'。首先,我的新數據庫.yml:

development: 
    adapter: postgresql 
    encoding: unicode 
    database: ticketee_development 
    pool: 5 
    username: ticketee 
    password: my_password_here 

# 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: &test 
# adapter: postgresql 
# encoding: unicode 
# database: ticketee_test 
# pool: 5 
# username: ticketee 
# password: my_password_here 

#production: 
# adapter: postgresql 
# encoding: unicode 
# database: ticketee_production 
# pool: 5 
# username: ticketee 
# password: my_password_here 

#cucumber: 
# <<: *test 

我的錯誤消息!

Someguys-MacBook-Air:ticketee someguy$ rake db:migrate 
rake aborted! 
database configuration does not specify adapter 

Tasks: TOP => db:migrate => db:load_config 
(See full trace by running task with --trace) 
Someguys-MacBook-Air:ticketee someguy$ rake db:migrate -t 
** Invoke db:migrate (first_time) 
** Invoke environment (first_time) 
** Execute environment 
** Invoke db:load_config (first_time) 
** Invoke rails_env (first_time) 
** Execute rails_env 
** Execute db:load_config 
rake aborted! 
database configuration does not specify adapter 
/Users/someguy/.rvm/gems/[email protected]/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:45:in `resolve_hash_connection' 
/Users/someguy/.rvm/gems/[email protected]/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:39:in `resolve_string_connection' 
/Users/someguy/.rvm/gems/[email protected]/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:23:in `spec' 
/Users/someguy/.rvm/gems/[email protected]/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:127:in `establish_connection' 
/Users/someguy/.rvm/gems/[email protected]/gems/activerecord-3.2.2/lib/active_record/railtie.rb:76:in `block (2 levels) in <class:Railtie>' 
/Users/someguy/.rvm/gems/[email protected]/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval' 
/Users/someguy/.rvm/gems/[email protected]/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook' 
/Users/someguy/.rvm/gems/[email protected]/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks' 
/Users/someguy/.rvm/gems/[email protected]/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:42:in `each' 
/Users/someguy/.rvm/gems/[email protected]/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks' 
/Users/someguy/.rvm/gems/[email protected]/gems/activerecord-3.2.2/lib/active_record/base.rb:718:in `<top (required)>' 
/Users/someguy/.rvm/gems/[email protected]/gems/activerecord-3.2.2/lib/active_record/railties/databases.rake:6:in `block (2 levels) in <top (required)>' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain' 
/Users/someguy/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:176:in `block in invoke_prerequisites' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `each' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `invoke_prerequisites' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:157:in `block in invoke_with_call_chain' 
/Users/someguy/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke' 
/Users/someguy/.rvm/gems/[email protected]defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run' 
/Users/someguy/.rvm/gems/[email protected]/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>' 
/Users/someguy/.rvm/gems/[email protected]/bin/rake:19:in `load' 
/Users/someguy/.rvm/gems/[email protected]/bin/rake:19:in `<main>' 

使用上面的database.yml,rake db:create:all RAN CORRECTLY並創建了我的表。但是,我的遷移仍然會失敗,並顯示與上面相同的堆棧跟蹤。

我改變了我的database.yml,以這樣的: 發展: 適配器:sqlite3的 數據庫:DB/development.sqlite3 池:5 超時:5000

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

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

更新:我完全改變了我的數據庫.yml這樣:

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

test: &test 
    adapter: sqlite3 
    database: db/test.sqlite3 
    pool: 5 
    timeout: 5000 

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

cucumber: 
    <<: *test 

現在,這是當我運行耙分貝會發生什麼:降:所有的,然後耙分貝:遷移。 rake db:migrate命令運行遷移,創建一個db/test.sqlite3並向其中添加一個表。所以,我的遷移仍在測試環境中運行。當我運行rake環境RAILS_ENV = development db:migrate時,同樣的事情發生,我得到一個test.sqlite3文件...

+0

我唯一的建議是不知爲何,你在你的env中設置了RAILS_ENV = test? – 2012-03-29 17:59:34

+0

好的建議,恐怕不是這樣。 – 2012-03-29 18:14:59

+0

你有沒有嘗試評論一切,但發展和看到會發生什麼?可能無法正常工作,但可能會給您帶來解決方案的錯誤。 – huntsfromshadow 2012-03-29 18:24:14

回答

2

我昨天夜裏開始完全相同的問題。不知道可能是什麼原因造成的,但最終找到了一個可行的解決方案。在內部配置/環境/ develop.rb的配置塊,我說:

Rails.env = 'development' 

我希望你的作品太

+1

config/environments/name.rb應該與database.yml中的名稱匹配。 Rails比配置更具約定。 – Sairam 2012-04-01 23:53:33

0

你在你的yml文件中缺少host: localhost

更改配置以這樣的:

development: 
    adapter: postgresql 
    encoding: unicode 
    host: localhost 
    database: ticketee_development 
    pool: 5 
    username: ticketee 
    password: my_password_here 
+0

我指定了主機,同樣的問題。請記住,我可以將遷移寫入上面的測試數據庫,但在任何情況下我都無法寫入開發。 – 2012-03-29 19:00:24

+0

我懷疑是空白問題。在vim中用'vi/path/to/database.yml'類型':set list'打開文件來查看所有不可見的字符。 – Josh 2012-03-29 19:16:48

+0

我剛剛嘗試過,我看到$符號正好在我希望看到他們感謝vi提示,但會派上用場。 – 2012-03-29 19:22:22

0

分叉的錯誤! 也許一些緩存或日誌問題它表明我們的代碼中沒有任何錯誤。

我的團隊解決它。

  • 刪除項目

  • 混帳克隆項目

  • Config中的database.yml

  • 啓動服務器

一切正常!

同一個項目的錯誤消失了! 所以我們認爲這是env問題或者cach或log。

3

有一個在 ActiveRecord的/ lib目錄/ active_record /任務/ database_tasks.rb一個 '輝煌' 的行:

environments << 'test' if environment == 'development' && ENV['RAILS_ENV'].nil? 

其中,直到2014年1月8日樣子:

environments << 'test' if environment == 'development' 
2

它聽起來像你的解決方案可能涉及一些事情。由於現有元素的原因,您的環境可能會導致遷移混亂。使用重置您的數據庫:根據您的環境

rake db:reset 

rake db:drop db:create 

,你應該能夠做到的遷移。

rake db:migrate 

PS - 您可能必須在每個命令的開頭添加bundle exec

祝你好運。

0

你可以用耙分貝:遷移RAILS_ENV = 「發展」

,或者你可以像使用

集RAILS_ENV =發展

耙分貝:遷移

相關問題