Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load `Rails.application.database_configuration`: Unknown alias: default

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

I found this Requesting a ruby-on-rails application gives Psych::BadAlias error but I didn't see a definite answer.

Here is what my database.yml file looks like...

  #  

  <<: *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

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.

like image 211
user3868747 Avatar asked Jul 24 '14 15:07

user3868747


4 Answers

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.

First, change &defaults to &default (singular); second, since you are using sqlite3 (as I am), replace the adapter mysql2 with 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
like image 51
hgorni Avatar answered Nov 14 '22 17:11

hgorni


your yaml file should contain a configuration that's default like the following

defaults: &default
  adapter: mysql2
  encoding: utf8

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

EDIT: Apr 22, 2020

  • change defaults to default to make it less confusing.
like image 4
jvnill Avatar answered Nov 14 '22 16:11

jvnill


jvnill's point on the right path

All you need is change it to default: &default instead of defaults: &defaults and you should be good.

like image 3
CocoHot Avatar answered Nov 14 '22 17:11

CocoHot


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)

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

like image 2
Mani Avatar answered Nov 14 '22 16:11

Mani