我创建了一个新的RubyonRails应用程序(我在Rails3中使用的第一个应用程序(,出于某种原因,我的应用程序似乎对它应该在什么环境中运行感到困惑(除非我只是感到困惑(。
当我运行rake db:create
时,它同时创建了"myapp_development"one_answers"myapp_test"数据库。当我随后运行rake db:drop
时,它会删除开发数据库,但不会删除测试数据库。
怎么回事?
编辑1:以下是我的database.yml文件的样子:
development:
adapter: mysql
database: myapp_development
username: root
password:
# 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:
adapter: mysql
database: myapp_test
username: root
password:
production:
adapter: mysql
database: myapp_production
username: root
password:
编辑2:尝试从头开始重新创建一个新应用程序,但仍然存在相同的问题。以下是我所做的。
1.创建了一个新的rails应用程序:
rails new myapp
2.编辑了我的Gemfile:
source 'http://rubygems.org'
gem 'rails', '3.0.6'
gem 'mysql', '2.8.1'
# Bundle the extra gems:
gem 'warden', '1.0.3'
gem 'devise', '1.2.1'
gem 'geokit'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
group :development, :test do
gem 'ruby-debug'
end
3.编辑了我的数据库。yml:
development:
adapter: mysql
database: myapp_development
username: root
password:
host: localhost
# 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:
adapter: mysql
database: myapp_test
username: root
password:
host: localhost
production:
adapter: mysql
database: myapp_production
username: root
password:
host: localhost
4.此时运行rake db:create
将创建myapp_development
和myapp_test
。如果我继续运行rake db:drop
,然后运行rake db:create
,我会得到一个命令行警告,说明"myapp_test已经存在",因为删除只删除myapp_development
,但创建尝试同时创建dev和test数据库。
只有在运行rake db:create:all
而不是rake db:create
的情况下,它才会创建开发和测试数据库。后者只会创建一个或另一个,这取决于您所处的环境。
检查config/database.yml
你的应用程序中的数据库配置文件,你可能想测试一下。但是database.yml将允许您访问所有内容。默认情况下,它将使用开发数据库,但您可以指定何时使用生产
同样需要注意的是,如果数据库不存在,只运行rake db:migrate
就会创建数据库并迁移模型,您可能需要使用