gpt4 book ai didi

ruby-on-rails - rake db :migrate and rake db:create both work on test database, 不是开发数据库

转载 作者:行者123 更新时间:2023-12-03 20:46:08 24 4
gpt4 key购买 nike

我是 Stack Overflow 和 Ruby on Rails 的新手。我的问题是,当我运行命令 rake db:create 或 rake db:migrate 时,测试数据库会受到影响,但开发数据库不会。

rails (3.2.2)

我的数据库.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=development。当我启动 rails 服务器时,服务器运行开发数据库。如果我运行“rails server -e test”,则服务器使用测试数据库。我仍然不确定为什么迁移在我的测试数据库上运行......

更新:
根据一个建议,我注释掉了除开发之外的所有数据库配置,现在出现错误。我正在运行 postgresql,我安装了 gem 'pg'。首先,我的新database.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/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:45:in `resolve_hash_connection'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:39:in `resolve_string_connection'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:23:in `spec'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:127:in `establish_connection'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/railtie.rb:76:in `block (2 levels) in <class:Railtie>'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:42:in `each'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activesupport-3.2.2/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/base.rb:718:in `<top (required)>'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/activerecord-3.2.2/lib/active_record/railties/databases.rake:6:in `block (2 levels) in <top (required)>'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/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/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:176:in `block in invoke_prerequisites'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `each'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `invoke_prerequisites'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/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/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/bin/rake:19:in `load'
/Users/someguy/.rvm/gems/ruby-1.9.3-p125@defaultGems/bin/rake:19:in `<main>'

使用上面的 database.yml, rake db:create:all RAN 正确并创建了我的表。但是,我的迁移仍然失败,堆栈跟踪与上述相同。

我将我的 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

更新:我完全将我的 database.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:drop:all,然后运行 ​​rake db:migrate 时发生的情况。 rake db:migrate 命令运行迁移,创建一个 db/test.sqlite3 并向其中添加一个表。所以,我的迁移仍在测试环境中运行。当我运行 rake environment RAILS_ENV=development db:migrate 时,同样的事情发生了,我得到了一个 test.sqlite3 文件......

最佳答案

从昨晚开始,我遇到了完全相同的问题。不知道是什么原因导致了这种情况,但最终找到了有效的解决方案。在 config/environments/develop.rb 的配置块中,我添加了:

Rails.env = 'development'

我希望这对你也有用

关于ruby-on-rails - rake db :migrate and rake db:create both work on test database, 不是开发数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9930361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com