Ruby-on-rails 无法加载`Rails.application.database_configuration`:未知别名:默认
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24937855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot load `Rails.application.database_configuration`: Unknown alias: default
提问by user3868747
I am new to Ruby on Rails and I am guessing that the answer to my question is pretty simple but I can't find it. I recently created a project and used "rails generate scaffold..." and everything was working fine. I wanted to add another column to the database so I used "rails generate migration ...." Everything worked well after this too. The problem started after I stopped the server and restarted it. Now I get this error
Psych::BadAlias
Cannot load Rails.application.database_configuration: Unknown alias: default
我是 Ruby on Rails 的新手,我猜我的问题的答案很简单,但我找不到。我最近创建了一个项目并使用了“rails generate scaffold...”并且一切正常。我想向数据库添加另一列,所以我使用了“rails generate migration ....”之后一切都运行良好。在我停止服务器并重新启动它后问题就开始了。现在我收到此错误 Psych::BadAlias 无法加载Rails.application.database_configuration:未知别名:默认
I found this Requesting a ruby-on-rails application gives Psych::BadAlias errorbut I didn't see a definite answer.
我发现这个请求 ruby-on-rails 应用程序给出了 Psych::BadAlias 错误,但我没有看到明确的答案。
Here is what my database.yml file looks like...
这是我的 database.yml 文件的样子...
#
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/development.sqlite3_test
production:
<<: *default
database: db/development.sqlite3_production
Here is the file that genereated after I did the migration
这是我迁移后生成的文件
class AddHardwareToComputers < ActiveRecord::Migration
def change
add_column :computers, :hardware, :string
end
end
and here is my schema.rb
这是我的 schema.rb
ActiveRecord::Schema.define(version: 20140723203054) do
create_table "computers", force: true do |t|
t.string "name"
t.string "serial_number"
t.string "user"
t.datetime "created_at"
t.datetime "updated_at"
t.string "hardware"
end
end
Any help would be appreciated. Thank you.
任何帮助,将不胜感激。谢谢你。
回答by hgorni
I know this is an old question but I was looking for the same issue and got it resolved using jvnill's + CocoHot's suggestion with a small correction. I don't have enough points to comment so I'm adding the amended solution as a new answer.
我知道这是一个老问题,但我一直在寻找同样的问题,并使用 jvnill 的 + CocoHot 的建议解决了这个问题,并稍作修正。我没有足够的分数来评论,所以我将修改后的解决方案添加为新答案。
First, change &defaultsto &default(singular); second, since you are using sqlite3 (as I am), replace the adapter mysql2with sqlite3.
首先,变化&defaults到&default(单数); 第二,因为你正在使用sqlite3的(如我),更换适配器mysql2使用sqlite3。
defaults: &default
adapter: sqlite3
encoding: utf8
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/development.sqlite3_test
production:
<<: *default
database: db/development.sqlite3_production
回答by jvnill
your yaml file should contain a configuration that's default like the following
您的 yaml 文件应包含一个默认配置,如下所示
defaults: &default
adapter: mysql2
encoding: utf8
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
EDIT: Apr 22, 2020
编辑:2020 年 4 月 22 日
- change
defaultstodefaultto make it less confusing.
- 更改
defaults为default使其不那么混乱。
回答by CocoHot
jvnill's point on the right path
jvnill 在正确的道路上的点
All you need is change it to default: &defaultinstead of defaults: &defaultsand you should be good.
您所需要的只是将其更改为default: &default而不是,defaults: &defaults您应该会很好。
回答by ImranNaqvi
I found a different case , actually i merged my old code with openshift's new code . And it generated two defaults and two development modes in the database.yml file . I just removed the duplicates and made my Yaml file look like below . and it worked (i'm pasting my yaml file for the sake of reference)
我发现了一个不同的案例,实际上我将旧代码与 openshift 的新代码合并了。并且在database.yml 文件中生成了两个默认值和两种开发模式。我刚刚删除了重复项并使我的 Yaml 文件如下所示。它起作用了(我正在粘贴我的 yaml 文件以供参考)
development:
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: admin
database: abc
timeout: 5000
production:
adapter: mysql2
database: <%=ENV['OPENSHIFT_APP_NAME']%>
username: <%=ENV['OPENSHIFT_MYSQL_DB_USERNAME']%>
password: <%=ENV['OPENSHIFT_MYSQL_DB_PASSWORD']%>
host: <%=ENV['OPENSHIFT_MYSQL_DB_HOST']%>
port: <%=ENV['OPENSHIFT_MYSQL_DB_PORT']%>
min_messages: ERROR
reconnect: false
As both of my enviroments required difference confs , so i removed the default case at all and defined individual case as you can see . It'll be good for some one to give it a shot when very desperate , i mean by removing default cases at all
由于我的两个环境都需要不同的 confs,所以我完全删除了默认情况并定义了个别情况,如您所见。有人在非常绝望的时候试一试会很好,我的意思是完全删除默认情况

