Rake db:setup or rake db:create applying wrong use

2019-05-25 00:21发布

问题:

Using Mac OSX Yosemite (10.10.4):

rails -v => Rails 4.2.3

ruby -v => ruby 2.2.2p95

Followed a combination of these instructions: https://www.digitalocean.com/community/tutorials/how-to-setup-ruby-on-rails-with-postgres and these instructions: http://blog.endpoint.com/2014/03/setup-rails-environment-with-postgresql.html

bash:

$createuser -P -d -e project (This was successful)
$password: aPassword
$re-enter password: aPassword
$rails new project --database=postgresql
$cd project/config/database.yml

Here is the config/database.yml:

default: &default
  adapter: postgresql
  encoding: unicode

  username: bespoke
  password: <%= ENV['PROJECT_DATABASE_PASSWORD'] %>
  #if I type env in bash => PROJECT_DATABASE_PROJECT: aPassword
  pool: 5

development:
  <<: *default
  database: Project_development

test:
  <<: *default
  database: Project_test

So far so good... but then:

rake db:create The first time: myusername already exists Second time: rake db:setup I believe they're supposed to be the same things...

myusername already exists
Project_test already exists

/Users/myusername/Challenges/Project/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /Users/myusername/Challenges/Project/config/application.rb to limit the frameworks that will be loaded.

So in other words the problem is: rake db:create or rake db:setup which seem to do the same exact were able to create the table Project_test hence they say Project_test already created... but they refused to create the table Project_development and instead insist on having me use the table myusername which is not listed anyWHERE~!

I have tried removing the default and hardcoding it into development to no avail. By the way here is output of psql=> /list

                                 List of databases
     Name     |  Owner  | Encoding |   Collate   |    Ctype    | Access privileges 
--------------+---------+----------+-------------+-------------+-------------------
 Project_test | project | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres     | mee     | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 myusername   | mee     | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 

Which proves that it did create the test database and used the correct Owner/user but it refused to create the development database.

What am I doing wrong or should I ask their github issues?

回答1:

Solved! It was an environment variable. First, I used env to see all the variables and noticed a DATABASE_URL was set.

$env
...
DATABASE_URL=postgres:///steve
...

So I thought, maybe the environment variable is taking precedent over <appname>_development (but not applied when generating <appname>_test) Then, I looked at my .zshrc file and saw where this value was being set:

➜  ~  cat .zshrc
# Added by Steve for permanent path vars
 export DATABASE_URL=postgres:///$(whoami)

Once I commented out this export line, then reloaded/restarted shell, running rake db:create generates <appname>_development as it should.

I don't remember why I needed to set that default but I'm glad I made a note in the config file, perhaps following a guide for using postgres client via the command line. Hope that helps someone else